#internals-and-peps
1 messages · Page 118 of 1
well afaik all hte time it's used its passed with an index, just that none of the methods available to pd.Index are used. A similar example - if something takes a pd.Series, but uses none of the pd.Series methods (just treats it at a list), then is it better practice to have the function take the most simple type (list) possible?
afaik, pd.Series is not a list, so typing it as list would be wrong.
again, Iterable is probably what you want.
yeah, you want the things in the category of Iterable, Sequence, Collection, etc
so instead of list[int] it's more common to use Iterable[int] ?
It really depends, it's a trade-off
This is why we can't have nice things. Because ducks are not types for quacks sake
you can either be more lenient in what you take (take the most minimal type possible), or be more restrictive in what you take
yeah - see being restrictive seems to make sense in a way as isnt that part of the point in typing things to start with?
If you're more lenient you make things easier for users, but if you decide to change your function implementation at some point, there's more of a chance you may want to have access to those other methods, which would require changing the type annotation, and having a breaking change
It just depends
depends by project or case? Is this not something people pick one approach with and stick to?
I wouldn't say it's more common to use Iterable[int] than list[int] necessarily, it depends what you are doing
ye, you have to decide on a case by case basis
no, it depends function by function
from https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#standard-duck-types
def f(ints: Iterable[int]) -> List[str]:
return [str(x) for x in ints]
i guess they're being permissive with the input and restrictive with the output?
however, Sequence[int] should probably be more common than List[int] for function arguments
that's what you see people in python often recommend, but I think they're not fully understanding the trade-offs tbh
remember that this means that everytime you change this function, you are limited to only returning a list, so you can't pass an infinite iterable to it
infinite iterable being a generator?
no
oh 🤔
infinite iterable is an iterable which never raises StopIteration
generators are a different thing, and are also iterables
but you can make an infinite iterable which is not a generator
e.g. itertools.count()
that's a reasonable point I guess but I still think that function is typed fine as written
it's a trade-off
I suppose a decent middle ground would probably be something like Collection[int]
Collection is something that suports size, iteration, and contains
i don't really know why i'd choose iterable over seq or visa versa
Sequence is a non-mutable List, basically
Use Iterable for generic iterables (anything usable in "for"),
and Sequence where a sequence (supporting "len" and "getitem") is
required
from the doc
So, Sequence API includes things like length, and random access
if you need random access or length, you use Sequence
if you are just iterating over it, then Iterable is sufficient
if you want to guarantee that it's a finite Iterable, then you could use Collection, although it doesn't seem like that's encouraged
it's tricky I guess because there are valid iterables that are finite, but are not Collection
as always, types cannot be exactly as precise as you want them to be, Iterable will include some things that are bad and Collection will exclude some things that are good
In python it's fairly common to use generators and generator expressions, and I think having false positives is worse than false negatives, so probably I'd use Iterable even in cases where it shouldn't be infinite
cool, i think i have a lot of list[int] to replace 😅
SOLID will tell you to always use the narrowest interface you can, but well, that doesn't really work in software that needs to be updated
FWIW I've used a ton of List and Dict all over my codebase (incorrectly)
well, if SOLID tells you that, SOLID is wrong 🙂
yup
if you're pretty confident you can live with the narrower interface, live with the narrower interface
but, if you aren't, then you should do exactly the opposite: stick with the wider interface
because you can always narrow the interface in the future, upon further reflection, or upon request, as it's not a breaking change
so minimise the chance of breaking changes, i guess
accepting the narrowest interface, that is. This gets a bit confusing 🙂 A function that accepts a narrower interface has a wider API, and a function that accepts a wider interface has a narrow API
which (to me) felt a little counterintuitive, as i thought mypy was meant to break everything lol and make it super strict
mypy is meant to enforce things for you, to the degree that you indicate that you need them
If you're not sure whether your function needs Sequence or Iterable, and you really don' tknow how the function will evolve, use Sequence
You can change it to Iterable in the future easily
However, if you are quite sure Iterable is sufficient, then stick with Iterable, there's no point forcing users to pass in Sequence if it's just unnecessary
one merit of iterable is that you can then do f(x for x in y if z), which is not possible with a Sequence, but alas, not a big loss compared to having to deal with changing the public API in the future
i find that mypy makes me use less comprehensions, stuff like l = [re.match(exp, s).group(1) for s in m] will give me Item "None" of "Optional[Match[Any]]" has no attribute "group, so I convert these to for loops and put a raise or whatever in the case of the match being None, idk what's best really tho
it's an engineering decision:
- how much headache does it cause for users to have a narrower API
- how quickly can you widen the API for users, if the narrow API is an issue
- how likely is your implementation to change in such a way that you want the narrower API
etc
@magic python you can use a list comprehension there still with a small helper function
T = TypeVar("T")
def unwrap(x: Optional[T]) -> T:
if x is None:
raise RuntimeError("Unwrapped empty optional!")
return x
l = [unwrap(re.match(exp, s).group(1)) for s in m]
i haven't understood these T yet 🤔 but ok cool that's interesting
a lot of other languages have syntactic sugar for this
T just means its generic
unwrap can accept an Optional[T], for any T
and it returns a T
why use T instead of Any
What would it return if you use Any?
I guess Any again 🤔
Right. That's why you use T.
so T does type inference?
Yeah, the T is inferred, and then can be used in other places
hm ok, cool
so if you pass unwrap an Optional[int], mypy understands that it returns an int
which is important
I didn't get what this means but ima going to look at it
Oh it was just a tongue in cheek comment related to duck typing
unfortunately this didn't work with
pd.Series(some_list).apply( lambda x: unwrap( re.match(exp, x) )
what do you mean exactly?
This:
import re
from typing import Optional, TypeVar
import pandas as pd
T = TypeVar('T')
s = ['one this is one here\n[two]', 'three this is three here [five]']
def unwrap(x: Optional[T]) -> T:
if x is None:
raise RuntimeError("Unwrapped empty optional!")
return x
# a
df_index = (
pd.Series(s)
.str.replace("\n", " ", regex=False)
.apply(lambda x: unwrap(re.search(r"^(.*) \[", x).group(1) ) )
.str.wrap(4)
)
gives:
check.py:21: error: Item "None" of "Optional[Match[Any]]" has no attribute "group" [union-attr]
the error still occurs for regular comp actually:
k = [
unwrap(re.search(r"^(.*) \[", x).group(1) ) for x in s
]
throws the same error as above
that's because the unwrap call is happening in the wrong place
I didn't look carefully enough at the original error
oh it shouldn't wrap .group
but you should get used to reading these errors and diagnosing them, if you want to use types. In this case, it's telling you that the thing you're calling .group on is an optional, right
right
you need to unwrap before you call group
is unwrap a convention for this sort of thing?
i mean, is this "unwrapping", hm ok cool
well, unwrapping is the noun, unwrap is the verb, and shorter, so better as a function name
A lot of languages don't have names for it because they have syntactic sugar
right, just id have used "raise_if_none" or something i guess as i've never heard of this
Kotlin for example would just do re.search(...)!!.group(1)
actually seems like python's none-aware operator proposal doesn't even cover this form anyway
typing is annoying sometimes but it has kinda forced me to think about what is actually going on a bit, which is probably healthy
yeah
I mean Optional is one of the most classic examples, because even some strongly typed languages tend to ignore it in their type systems
like Java most notably, and C/C++ in the context of pointers, which is pretty bad in C (but not as bad in C++, which has references)
Without mypy, you of course would just be calling .group on something that could be None, and it would blow up
mypy forces you to be explicit, in this case with unwrap it's still a not-terribly informative exception ultimately but at least people readin gthe code can see the explicit unwrap
You could also just define a local function similar to unwrap but with an exception message tailored to what's going on
yeah it only expresses something like python re.search(...)?.group(1)
I can imagine implementing custom Option[T] with its functionality, perhaps along with Result[T, E]
this is a question better suited for off topic channels, but this https://www.virustotal.com/gui/ works well
@magic python @halcyon trail
import re
from typing import Optional
import pandas as pd
def search(x: str) -> Optional[str]:
if m := re.search(r"^(.*) \[", x) is not None:
result = m.group(1)
else:
result = None
return result
s = ['one this is one here\n[two]', 'three this is three here [five]']
df_index = (
pd.Series(s)
.str.replace("\n", " ", regex=False)
.apply(search)
.str.wrap(4)
)
the "unwrapping" is kind of a red herring
i have a feeling you're going to run into the same problem you'd have with monad transformers, where there's no "general" way to unwrap a value that's in a monadic context
stated another way: you have to actually do the unwrapping in order to unwrap the thing
in a functional language (or python 3.10) you'd pattern match, in python you use if, and mypy knows the if x is None pattern and will refine the type of x accordingly
sometimes you have to refactor your code to make the types more amenable to analysis - 90% of the time this is a good thing and either forces you into better patterns or at least doesn't make your code worse
the other 10% of the time is when you're interacting with untyped code and/or untyped or recursively-typed data (which mypy can't handle yet)
what do you mean by a "red herring" ?
@paper echo
it's just a different approach
in the original code, they wanted to throw if something was None
oh, i missed that
still, i think trying to "generically" unwrap is an awkward abstraction in python
nothing, but i think it's an awkward abstraction
I'm just not sure what's awkward about it 🙂 Most languages (at least, those that aren't pure FP) hav esomething like this
Rust has unwrap, C++ optional has .value, Kotlin has !!
i guess i'd rather write that search function - the type unwrapping stuff falls out naturally from the logic
but isn't !! null-propagating?
ah
the point is that most languages that have an optional type try to let you do both
having to write that search thing by hand is pretty awful
fair enough, but that's because python has doodoo lambdas
i see the value, i guess i feel like "throw on null" is such a specific and uncommon thing to want and i'm surprised it's baked into a lot of languages
Yeah, I'd probably still prefer something taking a lambda in that case, usually you'll just be calling a pre-existing function perhaps with some arguments locked down, which isn't completely awful with python lambdas
propagating null is a lot more useful imo
it's not specific and uncommon at all
it's actually extremely common that based on other parts of the code, or your knowledge of the input, that you know that nulls are not possible, or represent a grave error, and you simply want to throw
Actually, an unwrap with optional msg and exception_type arguments would be extremely useful in python and cover a lot of situations
Propagation is probably more useful overall, at least in Kotlin I found myself using propagation more, even though Kotlin supports both conveniently, and uses exceptions still quite a bit
But partly that's because, in the first place, Kotlin functions tend to throw for grave errors.
In Rust where everything is Result, for example, I tend to see quite a lot of unwrap. But I guess taht's not null, so I'm not sure if you'd include that.
def unwrap(x: Optional[T], msg="Unwrapped empty optional!", exception_type=RuntimeError) -> T:
if x is None:
raise exception_type(msg)
return x
def map_optional(x: Optional[T], func: Callable[R, [T]]) -> Optional[R]:
if x is None:
return None
return func(x)
k1 = [
unwrap(re.search(r"^(.*) \[", x), f"Regex did not match {x}!").group(1) for x in s
]
k2 = [
map_optional(re.search(r"^(.*) \[", x), lambda x: x.group(1)) for x in s
]
Neither is super beautiful but it's the best we can do I guess
That's interesting because I made an update to production code to propogate nulls in cases where computations were unreliable, and it wound up breaking a lot of things. I'm brainstorming ways to handle that, but I am not sure what I can do other than maybe adding a decorator that checks if any of the positional arguments are null.
Otherwise I need if statements/try-except clauses everywhere which just seems bad
this isn't a code specific question
but a project question.
can i use sqlite3 to create a database that stores scores from throughout marking periods for gpa purposes can i create a table using the user input that i get from within the program?
Not sure if this is the channel for this, this is more of advanced discussion regarding the language itself. However, I don't see why you wouldn't be able to do what you just described.
ok thanks
What are we discussing today?
@pseudo cradle well, i guess it really depends what happens before you propagated the null, what exactly "unreliable" means
in many cases but not all breaking loudly is preferable to brekaing quietly
as to what can be done, well, mypy will handle this for you, right
if you have an argument annotated as int and you have another function that returns Optional[int], you won't be able to call that function and pass it directly to the argument
I don't think I've ever used Optional before
So in the mypy world, as soon as you change a return type to Optional, it should be able to find all the breaks via static analysis, unless you are doing fairly fancy stuff
Hmm
do you use mypy?
No. I probably should
or pyright
I'm a digital signal processing engineer, while I try to filter messy signals sometimes I have to remove them altogether if they're bad enough. I added a decorator that makes it so if there's not enough good signal for a calculation, I just return None. But sometimes I call those functions within other functions, that's where it's breaking and I am not sure how to handle it.
why not raise an exception and catch it at a step where it makes sense to try the next signal
Mostly because it would be a lot of try and excepts, which I feel like would clutter our pipelines.
I'm trying to avoid that, if possible.
Ideally there would be a way to have a try and except clause for each line in a function without having to manually write all that out.
I tried replacing the bad data with numpy NaNs and then use nan related functions, and that sort of works but has its own headaches
Suppress?
No context here, but the contextlib_ext module has a decorator that tries a function and returns Nome if it catches an error
I wish it had a fallback decorator though. Try this, if it fails, do that
That shouldn't be too hard to set up yourself though, right?
but what would suppressing buy you, that would be the same as not signalling the error at all
unfortunately there probably is no real silver bullet, handling errors is hard, especially if you have many points of failure.
Pick your poison based on what makes sense. Throwing can be pretty good because you can do many operations without constantly looking for optional/nan, and catch at a slightly higher level
^ the ideal thing here would probably be conditions, you could just set up a restart that would pick the next signal instead, but I haven't seen a good impl in python yet
can you explain what you mean by that?
are you familiar with conditions as a form of error handling, afaik the only language that actually implements them rn is common lisp, but they were in very early versions of rust, but got removed.
no
apologies for the delay, went for dinner.
The idea of a condition is that rather than working like an exception and unwrapping the stack, it instead calls a so called "restart" immediately (essentially a callback), which then decide what to do. For example, you can set things up such that a NameError would use some default fallback (not a great idea) instead of truly erroring out. By default, there are 2 restarts - act like an exception and kill the program, but you can add more at runtime dynamically. You can always do things a condition can do without a condition, but conditions are just kind of convenient. You can even unwrap the stack partially and do something at some different part of the call tree.
the problems are obviously complexity and not being checked at compile time, which is why rust went with result types instead
afaik a condition is pretty much a delimited continuation
pretty much
but continuations are rarely the correct tool, since they are far too flexible
(or at least, they're conceptually similar, even if they're not technically continuations under the hood)
for example, scheme cannot have the equivalent of a context manager that will always call close, like unwrap-protect of CL
i tried to implement something like this with generators, but as soon as you wanted to actually use generators in your code, it kind of blew up
exactly due to continuations
lisp people love the lisp condition system
i'm with them, i feel like it's the right balance of power and usefulness
yeah, I understand why rust decided against it, but it is a nice system
same
though the simplicity of exceptions maybe has its place more
even if they are sometimes unwieldy to handle
can't you just ignore the restart functionality?
but i guess that means more complicated "professional" code
I feel like a large convenience of conditions is being able to test them out interactively in the REPL, which is somewhat incompatible with the idea of ignoring restarts by default
I feel like I'd need to see an example to really see where the benefit lies
does this callback have to be passed explicitly into every function, is it a global thing that you can temporarily change in a certain context, how does it apply to functions getting called transitively, etc
imagine you could do this @halcyon trail :
def f(x):
try:
print(x[5])
except IndexError as exc:
exc.restart(None)
assert f('hello world') == 'o'
assert f('') == None
Example from https://gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html with my docstrings added:
(defun well-formed-log-entry-p (text)
"Is the log entry well-formed?"
...)
(define-condition malformed-log-entry-error (error)
"Condition signaling that a log entry is malformed."
((text :initarg :text :reader text)))
(defun parse-log-entry (text)
"Parse a single log entry. This should be a single line of text."
(if (well-formed-log-entry-p text)
(make-instance 'log-entry ...)
(error 'malformed-log-entry-error :text text)))
(defun parse-log-file (file)
"Parse a log file, with one log entry per line."
(with-open-file (in file :direction :input)
(loop for text = (read-line in nil nil) while text
for entry = (handler-case (parse-log-entry text)
(malformed-log-entry-error () nil))
when entry collect it)))
the python equivalent (using the hypothetical exc.restart) would be something like
def is_well_formed_log_entry(text):
"""Is the log entry well-formed?"""
...
class MalformedLogEntry(Exception):
"""Condition signaling that a log entry is malformed."""
def __init__(self, text, *args):
# I don't know how to do this "properly" for custom exceptions
self.text = text
super().__init__(text, *args)
def parse_log_entry(text):
"""Parse a single log entry. This should be a single line of text."""
if is_well_formed_log_entry(text):
return LogEntry(text):
raise MalformedLogEntry(text)
def parse_log_file(file):
"""Parse a log file, with one log entry per line."""
entries = []
with open(file) as fp:
for text in fp:
try:
entry = parse_log_entry(text)
except MalformedLogEntry as exc:
exc.restart(None)
if entry:
entries.append(entry)
return entries
what does exc.restart do?
Okay, I think I understand what this lets you achieve over exceptions/results
whether it's actually a good idea to let you have that much flexibility seems more questionable (to me)
the problem is that once this becomes your error handling strategy, you basically have inversion of control, everywhere, implicitly
what do you mean everywhere?
at every exception, yes
or more generally, at every signalled condition
but the point is that your code now explicitly doesn't know what to do anymore, and it's asking for help from "higher up"
in python, the only thing you can do is unwind the stack and exit
when you add types into this mix, you get the "algebraic effects" i was going on about a while ago
i mean yes, theoretically, you can write code with a restart that causes the original code to summon the dreaded nasal demons or launch nuclear missiles, but you can do that with except anyway
except doesn't let you inject code downward
the exception is thrown upward
if you want some kind of handling on errors, you need your function to take an argument explicitly (or use a global, which we know is bad, mkay)
my very personal opinion about where this will end up, I think, is towards more static approaches towards error handling (similar to Result), but properly supported by tooling. I haven't seen any language really come close to this, but if I had to make a guess for the distant future, that would be it.
the main complaint against Result like types is that in some cases it can make bubbling up a newly added error tedious, but an IDE ought to be able to do such a thing automatically
i guess i'm not sure what the problem with injecting "downward" is
if someone goofs up and injects garbage into your function, that's on them
maybe your concern is that it effectively expands the "api surface" of a function?
i've said this before, but i have a theory that common lisp gets away with these ridiculous features because common lisp programmers are unusually disciplined
(other than the let-over-lambda people)
idk, I think CL gets away with it because lisp is almost never used in large scale projects
most people use CL on hobby projects or on their own, and love it. I have seen horror stories from people who worked with CL at companies and had to deal with the worst kind of code
other peoples' code 🙂
The problem with injecting downward for starters is that it violates referential transparency
(unless you inject downward explicitly, via a function argument)
Causes mypy to generate a text file report documenting the functions and lines that are typed and untyped within your codebase.
from : https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-linecount-report
Which gives output like:
1286 13487 368 866 total
286 386 13 14 something.somewhere
...
there are no headers in the file though for the columns, so presumably for module something.somewhere we have 286 lines typed, 386 lines which aren't typed, 13 functions which are typed, and 14 functions which are untyped?
it's also not too clear what the definition of "precision" is wrt mypy 🤔
My guess would have been that it's 286 lines out of 386 are typed, and 13 out of 14 functions
yeah that's mine from the wording, cheers... no headers in a table tsk
maybe. the algebraic effects stuff is apparently useful and important enough to have found its way into the implementation of multicore ocaml. so maybe with types involved there is some additional safety
just a simple question
flutter or react-native
im getting into mobile development and im fairly new to dart, whereas im fairly experience in js but I want to work with regard to the future so what should I learn?
!ot @past ingot this channel is about the python language itself, as per the channel description. I think your question is better in an off-topic channel.
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.
Anyone happen to know when __floor__ and __ceil__ were added to floats? I know it was after 3.6.8 
Apparently in 3.9 
@unkempt rock when are those used?
with math.floor and math.ceil
Whats with all the people posting assignment questions? Its July, isn't it break for most schools?
How do you make a number from two integers where the second integer is supposed to be the fractional part of the number?
Is there a more elegant method than a + (b / 10 ** math.ceil(math.log10(b)))?
Or should I just do float(f'{a}.{b}')?
This seems to work, but I think the float method may work more efficiently. What do you think?
the float method may work more efficiently
You can always benchmark 🙂
Also,logshould be a pretty long operation
Why do you need to do this operation?
seems like the ceil + log is slightly faster, but IMO the formatting way is easier to understand
Yeah, hmm...
I just have a regex that matches a numeric value, but splits the integer and fraction part to two separate groups
The ceil + log is faster but less consistent with the time taken?
You can just take the group 0 and put it into float, right?
or just capture a separate group for the whole literal
There's some clutter as well so I can't just get it
Some additional prefix and suffix
if you had the string of b from a regex, calculating the log base 10 would be a lot easier!
Hmm, yeah
The float method is actually faster tho, if you start with string instead of integer
is it possible to build a calculator out of string
by i mean out of string i dont mean eval() or exec()
wouldn't it be a lot larger than the original int max value?
Could you elaborate on your question? What do you mean
float(f'-{number}') seems to be slower than -float(number)
Probably because of the string allocation/formatting
Yes. Just the sign bit
Does Python do copy-on-write for strings?
Like if I want to reference a huge chunk of string in many places, would Python duplicate that exact string or just reference it?
everything is passed by reference
there are no modifiers to arguments like in other languages because it's all objects so they're treated the same (as opposed to e.g. C# where it depends on whether we have object or basic type) - as references
that's why we can mutate arguments inside functions
strings get copied, you cant have 2 inequal strings using the same part of memory
Copied when modified right?
strings are immutable, so probably yes
Isn't it technically "pass by object-reference" rather than strictly "pass by reference"? I've read some pieces on this but I have to admit my CS fundamentals aren't really strong enough to know how important that distinction is
i've heard "pass-by-assignment"
I have heard pass by value reference as well
it's often better to just explain the semantics than to try and come up with the perfect name
yeah definitely, it's much more detailed than just a simple phrase
the terminology makes it tempting to find the corresponding equivalent in cpp but there isn't one as far as I'm aware
Is there an example of where it would trip someone up who was expecting it to be traditional "pass-by-reference"?
oh, right c/cpp "reference" passing which is basically passing the pointer itself and overwriting stuff at that exact memory space...
def add_one(a):
a += 1
```vs
```cpp
void add_one(int& a) {
++a;
}
```these functions work differently, but I haven't seen too many bugs due to this
Because if a=1 in Python a outside the function would be 1 still but in the bottom one it ends up as 2?
yes
but more specifically, a is a whole new name, so changes to a won't affect any names outside the function
but you could always do
def add_one(l):
l[0] += 1
my_number = 1
tmp = [my_number]
add_one(tmp)
my_number ,= tmp
!
(it is gross)
yup, that would work
yep, that's how wrappers work - because wrapper's reference doesn't change, only references stored inside the wrapper do
Python is pass by assignment. I think the cleanest way to explain it is to just explain it, the lower level abstractions don't easily explain python behaviour
If I'm not mistaken, the closest lowest level abstraction would be if the code was doing pass by value, but every variable in the code was a first level pointer..... I would much rather just say pass by assignment and call it a day.
not quite, you can do *ptr = value with a pointer
you can't do that with a python reference
Yes, you were also not allowed to increase or decrease reference levels
Only a first level pointer
it's fascinating to me how this idea persists that there are only two choices: pass by value or pass by reference (with people not even agreeing on what pass by reference means).
Yeah, I see people keep correcting each other on what it means
well, like lots of descriptions of programming languages, the term gets used slightly differently in different contexts.
"reference to a value" or "reference to a variable"?
Yeah. More standardized terms would help
!warn 798380170586882109 Your first message on the server being an off topic meme in an on topic channel is not appropriate. Please reread our rules before engaging further.
:incoming_envelope: :ok_hand: applied warning to @unkempt rock.
This is not a help channel, please see #❓|how-to-get-help
I was just thinking whether to pass the actual string or create an object which holds the string and pass the object to reference the string. I'm not manipulating the string outside of getting some slices and whatnots so I don't want multiple copies of the same string.
the slices will be copied out, so if you don't want that, you could make your own string type, but I wouldn't bother
python caches strings to save on memory as well
I remember taking it as a given that in GC languages with immutable strings, that substrings wouldn't make copies
but then learned that in Java, they actually changed it back from not making copies, to making copies
usually though in python I wouldn't worry about something so small, unless your strings are truly huge
if your strings are truly huge then you may want a different data structure than the built in str but that's a pretty extreme case
I'm currently developing a simple resnet 50 for a regression which takes in a input image and predicts the target value
but when I try predicting test dataset i always turn out to get the same value
so im not sure, if anyone can point me to some resource that'd be great
Has anyone ever seen a "MultiException" pattern out there? I was thinking it'd be nice in complex input validations, like CSVs, to have individual exceptions per error, with their own error messages and sets of stackframes, line numbers, and file numbers, but create and aggregate these functions during the course of validation, then raise a combined mega-exception at the end with all the individual exceptions as a collection.
Because conceptually, I want to give the end-user all the errors I can find, and not just the first one, so they don't have to upload the same dataset over and over and over with one error at a time fixed. Sure, maybe some of the downstream errors are spurious because of an upstream error, but for the most part at least in my case it'd be valid
I usually just return lists of errors in those cases
Yeah, I do too
I have dealt with pretty much exactly the seem use cases, i have regression tests, some failures are hard failures and it ends the regression test, but others are not, so I add the failure to a list, and continue
and then return the list
the most useful thing I can contribute to this kind of situation, is a context manager that takes a list, and pushes any errors into the list
but I was just thinking "what if it had the whole exception context like a regular exception" so that when an engineer got of an end-user's error, they could trace exactly where in the code that the error occurred and recreate the context, easily, rather than searching for exception messages or something
I seem to remember this in a PEP of sorts
err_list = list()
...
with append_errors(err_list) as e:
...
# If this is one of the errs where you *do* need to early exit
if e:
return err_list
...
what do you mean by a macro?
adding line and file to the error creation
Oh, there are a few packages out there for making macros. none are standard and I'd rather avoid them
but I know it's possible
I didn't think you needed a macro to add line and file
but at any rate, in the example above, the code inside the context manager can just throw an exception as usual
and the context manager can easily save all the traceback information for every error
Well, you don't. but if I just do errors.append(ExceptionClass("etc etc etc"))) I'm not going to know where ExceptionClass got constructed and "thrown"
yeah, okay, you're right.
I didn't think of wrapping that in the context manager
In my head that'd add a level of indirection but it really doesn't have to, does it...
yeah, it's pretty weird but context managers are a pretty good tool for handling error management related things in python
thanks!
things probably shouldn't be that way but they are
sure, np. what do you mean by a level of indirection?
oh, i'd want to pop the first frame off the stack when saving that info, because the first frame would be "in the context manager"
I dunno. Until I actually sit down and write it, it's kind of sitting in a mashed up jumble in my head. I may be wrong.
But I'm on the right track now, so thanks
Oh wait, yeah. I think I get it now.
!pep 654
^ there's a proposal to add that to the language
@azure oar It wouldn't be in the context manager
you'd throw an exception from some code, it gets caught in teh context manager, the traceback of the exception starts where it was thrown from, not in the context manager
In CPython, it needs to make a new object, if only to store a new pointer into the original string and a length for the slice. That new object needs its own reference count. Unless the substring is huge, I'm guessing that the thought is that the memory allocation is already pretty slow, and the cost of copying data out of the original string is low compared to the memory allocation, so it's not worth optimizing.
Sure, a new object would need to be made in every language, hard to see how that's avoidable
I think it's not just that, another issue is "memory leaks", if you have a big string allocated that way, and now you have all these substrings pointing into it, then you need to keep the big string around
Yeah I figured it out... Thanks y'all
Another thing is that it just simplifies the implementation, which saves you perf in general. Anyhow I just thought it was interesting, it does say something that Java did avoid the copy for a long time, and then switched, and obviously a lot of thought/man hours went into that, so I think it's not super obvious which is better, perhaps
np
The biggest issue with having slices reference the original string in Python would probably be that the string caches its own UTF-8 representation, and so that would need to be referenced as well - and that's not trivial, since it's multi-byte
Though I suppose you could reference the buffer of Unicode codepoints and not the buffer of UTF-8 bytes, and just lazily populate the latter instead
Though implementation complexity would definitely be a big consideration as well. The implementation is already much more complicated than you'd naively think.
Between caching the UTF-8 representation, and storing the array of code points in different widths depending on whether all of the characters are in ASCII or in the BMP
Has anyone worked with __class_getitem__?
Documentation and examples on it are really sparse
I know of it, but I've never needed it for anything. As far as I know it's really only used for generics
And the implementation is usually just that the class returns itself
i don't think there's even a point in using __class_getitem__ unless you want to confuse the hell out of people. mypy won't know what to do with it anyway, right?
i use it for some class constructors
(MyArray*5)[int] would return MyArray_5_int for example
In [141]: Range[2:]
Out[141]: Range(2, ∞, start_inc=True, end_inc=False)
In [142]: Range[2:] ^ Range[6:]
Out[142]: Range(2, 6, start_inc=True, end_inc=False)
This was just so i could use [] instead of () cause i don't like typing parens
I have no idea
But I was thinking of using it to use annotations for some custom system
We have some dataframe subclasses and I want to use it to indicate we want to slice these columns out of the instance of this class before passing it to this argument
Total abuse but like idgiaf
Typing is a fool's errand anyway
Basically
def foo(a: A[[1,2]]):
...
Upon inspection we would have (A, [1,2]) and then we do A(<data passed to a argument>)[[1,2]] when we call foo
it was recently brought to my attention that "annotations" were originally not meant only for types, but for generic metadata
so it's not abuse as such
but maybe you want to rely on the Annotated type https://www.python.org/dev/peps/pep-0593/ to avoid conflicting with or confusing type checkers
i was at PyCon 2007 in Dallas where Guido gave a keynote about "Python 3000". When he couldn't remember the word "annotations," he said, "you know, the things that aren't type declarations..."
oh thats amazing, I didn't know of this.
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
res, dups = set(), set()
seen = {}
for i, val1 in enumerate(nums):
if val1 not in dups:
dups.add(val1)
for j, val2 in enumerate(nums[i+1:]):
complement = -val1 - val2
if complement in seen and seen[complement] == i:
res.add(tuple(sorted((val1, val2, complement))))
seen[val2] = i
return res
how is the answer returned as a List of list(s) here?
why not a set?
who said it returns a list of lists?
the python prompt lol
are you asking how to make it return a list of lists? Or are you asking why it returns a list of lists?
why
it doesn't return a list of lists. it returns a set. Why do you think it returns a list of lists?
can you show us the output you are seeing?
That could be their test runner converting your result into a list of lists. Try adding print(res) just before your return statement
print gives {(-1, 0, 1), (-1, -1, 2)}
that's really odd for them to convert it at run time tho
@unkempt rock it's also odd for them to insist that you put your code into a class called Solution. This smacks of Java.
this is so they can create an object to run their test cases I believe
Python doesn't need you to do that. They could have simply used a function you wrote.
also sometimes you need to instantiate or initialize a data structure that's why
there's no reason for them to force you to use a class.
you didn't use self once, because there's no point to the class.
@unkempt rock right, leetcode always gives you a class to fill out. it's kind of silly, but harmless.
So I like this but I think the syntax is a little cumbersome for my application. What if __class_getitem__ returned Annotated[cls, key]?
from typing import Annotated
class Foo:
def __class_getitem__(cls, key):
return Annotated[cls, key]
They give a class with a single camelCase method 
Strangely enough, in Rust they provide a snake_case name, but that's because the compiler will give you a warning if it's in camelCase
And in TypeScript, they don't require you to make a class?..
anyway, sorry for the off-topic
Maybe that's since classes were introduced in ES6, they aren't valid in pre-ES6 environments
TypeScript had classes a long time before ES6
I still don't understand why Python solutions require a class. "It's an OO language, so we have to stick a class somewhere in there"?
Oh wait, nvm, thinking of the wrong feature
Hello. Does anyone interested in discussing a project and nosql db architecture I'm working on?
I mean, real discussion, if anyone's available
@terse orchid it works best to start talking, and see if people join in.
You are right
I just didn't wanted to occupy the channel 🙂 anyway;
The project is about to collect various data about companies from the internet (public data), and let the customer monitor social media posts and news about both themselves and their competitors
The thing is this project is already working. But it got too complicated. Because there are different sources and customers may have different demands. It (the db) has to be very flexible. One tough point is, two companies may conflict. I mean, both of them are using the product and both of them are competitors to each other
Currently the project is running with mongodb as database, flask as API server. That's pretty much it
I know it's hard to join the topic, it's okay. I just wanted to share because I'm a bit overwhelmed 🙂 It's all right if nobody answers
I wrote a dependency injection library with which you can register certain types in a container and automate instantiation of them
Example:
# container.py
import main
from python_di import containers, providers
class Container(containers.DeclarativeContainer):
a = providers.Factory(main.A)
b = providers.Factory(main.B)
container = Container()
container.wire(modules=[main])
if __name__ == '__main__':
main.function()
# main.py
from python_di.wiring import inject
class A:
pass
class B:
def __init__(self, a: A):
self.a = a
@inject
def function(a: A, b: B):
print(a, b)
If any dependencies have type hints in their signatures (__init__ in case of classes) they would be provided from container
Also dependencies could be registered as abstractions, so for example:
Factory(IService, Service) if some of classes would need IService Service would be provided instead.
Is there anything i can add into it?
@tidal marten By function signature type annotations
Also all providers should be registered in Container directly or via Module,
Providers on the other hand accept type and callable
class Factory(TypedProvider, Generic[T]):
def __init__(
self,
type_: Type[T],
factory: Callable[..., T] = None,
**kwargs,
):
And factory defaults to type_
Ohh, so the signature actually have reference to the original object/class. That's neat...
It would not work if you're importing via if TYPE_CHECKING block, but that would be impossible to inject too 🤔
Plus you could avoid that using abstract classes
Yeah, annotations contain references to actual types
Well, I haven't had any need for such thing (in Python). But I have encountered it in Angular.
Yeah, that's something similar
Probably if you have a system that needs abstraction like Angular, then sure
Most systems could benefit from abstractions i think
I'm just wondering whether setting up this abstraction is worth the additional boilerplate stuffs
There's really no boilerplate i think 
You don't have to create interfaces, you could register classes as-is if you want*
What about the initialization though?
Hm?
My understanding (for Angular at least) is that the dependency injection is used to inject dynamic state object into the class
So I'm wondering if this is the case with your library
Like for example, if I want to do this with Flask, I'd do something like:
@inject
def route_index(route: Route):
...
and the route would be filled with the current route data and utilities
Hm, i don't have much experience with flask though
What is that Route class?
afaik most of flask objects are thread-bound, right?
Well, in actuality, Flask use decorator to register a route and it will just pass in some argument to your corresponding function, so it doesn't actually use dependency injection I think.
maybe not automated
but the idea of dependency injection is really just that whatever needs stuff gets it passed to it rather than making it itself
Anyway, flask would pass route parameter to your function if you declare it in it, right?
That would be some sort of DI too
Hmm, let me see. Maybe this dependency injection can be used for something like:
@inject
def handle_request(request: Request, db: DatabaseConnection):
...
So the dependency injection would automatically supply the Request and DatabaseConnection objects automatically like that?
If they're registered in a container - yes
But your factories for those injected data would need to depend on some sort of a global/singleton then
I mean, that could work
What singleton are we talking about?
container? Yeah, there should be only one 🤔
But it doesn't have to be this way
Like for DatabaseConnection, you will need to have a pool of connection stored somewhere
Yep, this could be a singleton
So in your __main__ you initialize DatabaseConnectionFactory with some database connection, then register the factory to the container?
class DbConnection:
pass
class Container(containers.DeclarativeContainer):
db_connection = providers.Singleton(DbConnection)
More like this 
Not sure if I like the implicit nature of this, lol. But this can be useful if you have a massive project.
Since both container and request handler would run in same thread you could probably just get flask.globals.request
Maybe they should be explicitly marked for injection then
Yeah, but using globals are kinda meh (in Flask-way)
@inject
def handle_request(
request: Request = Provide,
db: DatabaseConnection = Provide,
):
...
I think you would have to register it in a container and that's all
class Container(containers.DeclarativeContainer):
db_connection = providers.Singleton(DbConnection)
flask_request = providers.Factory(Request, lambda: flask.request)
# or since it's a proxy maybe we could do it this way
flask_request = providers.Object(Request, flask.request)
So, in this situation, the function handling the routing does not need to pass the arguments. But, the function, through the decorator, can request what things it needs.
Yep
For each argument if such type is registered in a container it would inject it
And, for instance, Flask may only provide Request and Response injection. I assume you can add third-party module to provide e.g. SQLAlchemyConnection or RedisConnection as well?
But in this instance you need to control the initial declaration of the injection (the Container), I assume
If you just register these connections in a container it should work fine
Also since you need some sort of arguments for them e.g. host, port you could provide them as well
Quite neat
from python_di import containers, providers
class RedisClient:
def __init__(self, host: str, port: str):
pass
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
redis_client = providers.Singleton(
RedisClient,
host=config.redis.host,
port=config.redis.port,
)
container = Container()
container.config.from_dict({
"redis": {
"host": "localhost",
"port": "I Don't know what port redis uses by default :)"
}
})
I assume the injector instantiates new object for each function call as well?
Yep, if they're not declared as singletons
But they're reused during single injection
@inject
def function_to_wire(a: A, b: B) -> Tuple[A, B]:
return a, b
def test_instances_are_reused_during_single_injection(module_to_wire):
container = _Container()
container.wire(modules=[module_to_wire])
a, b = module_to_wire.function_to_wire()
assert b.a is a
class A:
pass
class B:
def __init__(self, a: A):
self.a = a
That behavior is easy to change by implementing different resolver class
Container is merely a storage for providers, all work for resolving types is delegated to Resolver 
I made this design using turtle:
Thanks! It uses the turtle circle function and I programmed it to where every-time a circle is made, it will move the turtle up one pixel and it increases the size of the circle by 1.
Nope, I just hold in one hand and the computer in the otber.
Here’s the code btw:
import turtle as t
def a():
t.hideturtle()
t.penup()
t.goto(0, -255)
for i in range(0, 1):
for o in range(0, 1000):
t.pendown()
t.speed(1050)
t.circle(o)
t.penup()
t.speed(1050)
t.circle(o)
ax = “black”
t.color(ax)
t.penup()
while True:
a()
Hey idk if this is the right channel to ask this, but is there some lib which I can use to convert a gif to apng?
Like if no lib then any piece of code example would be lovely too
https://stackoverflow.com/a/48165615
Imageio can read a gif as a list (if the gif is animated) of images/ndarrays and with cv2.imwrite you can write the images to png
I can read jpg file using cv2 as
import cv2
import numpy as np
import urllib
url = r'http://www.mywebsite.com/abc.jpg'
req = urllib.request.urlopen(url)
arr = np.asarray(bytearray(req.read()), dty...
I think ffmpeg could also be of help
@sage siren
Wow thanks, I will take a look : )
Hello guys, I do not know if this goes here or not, but I am looking for some library that focuses on optics (I am working with mirrors) and another in solar resources (Ray Tracing, Sunshape, ...). I hope you can help me in my search: D
if A[i] != x and B[i] != x:
how would you use De Morgan's laws to translate this
into using "or" (making it more compact)
or is it not possible to make it much more compact?
not a and not b == not (a or b)
not ((A[i] != x) or (B[i] != x)) ?
Don't make it compact, it's readable
That's what matters
or easier: x not in {A[i], B[i]}
that assumes hashability - use a tuple, not a set.
true
probably faster than making a set anyway
that too.
if x is not in either A[i] or B[i]
de morgan's law would be if A[i] == x or B[i] == x, but if x in (A[i], B[i]) would be the more succinct, readable way to write that.
huh, what?
also means if you change the variable name, you're not altering a massive line of variables or if you want to add another thing to have x not in
er, I missed a not in both of those.
ah, makes more sense then
de morgan's law would be if not (A[i] == x or B[i] == x), but if x not in (A[i], B[i]) would be the more succinct, readable way to write that.
should be that, I think 🙂
the first was already good, tbh
yep. OK, third time's the charm.
isn't it not (A[i] == x or B[i] == x)?
stupid de morgan
oh well. optimize for readability, not code size
of the 3 choices, either if A[i] != x and B[i] != x: or if x not in (A[i], B[i]) seem most readable.
if not (A[i] == x or B[i] == x) is definitely harder to wrap your head around.
oh that makes sense
the != x was confusing me
this makes sense to me
if you translate it into English
"if A[i] or B[i] doesn't equal x"
but yea the doesn't part is on the outside
It depends on what the "condition" is i think
How do you know if Python really uses like 1GB of RAM or it's just a bad case of memory fragmentation? Lol
An empty Python interpreter uses about 30 MB of RAM on my machine. You have a memory leak or an inefficient algorithm in your program
I'm parsing a 60'000-line file into some sort of tree, lol
And it took like 12 second for the whole tree to be constructed
I had a function I was supposed to rework take 3.5gb of ram to run because it was trying to hold 30k nested dicts from an api call in memory, after I changed it to paginate and yield only the important bits it dropped to 250mb
Most of my nodes only hold the start and end index of the initial buffer though, so I expect that there should be no duplication issues. Maybe some nodes hold onto regex match objects and that have duplicate substrings?
Is there any profiler that I can use to try pin things down?
need help solving this can anyone help me with the code
https://drive.google.com/file/d/1QUieumGI3wIIY-M9Av5jzyiskPs1eEnl/view
Hello. Has anyone ever worked with Python on webrtc?
can anyone pls tell that pandas datareader for yahoo.finance is working or not?
Traceback (most recent call last):
pandas_datareader._utils.RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/TSLA/history?period1=1356993000&period2=1577917799&interval=1d&frequency=1d&filter=history
Response Text:
b'<!DOCTYPE html>\n <html lang="en-us"><head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8">\n <meta charset="utf-8">\n <title>Yahoo</title>\n <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui">\n <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n <style>\n html {\n height: 100%;\n }\n body {\n background: #fafafc url(https://s.yimg.com/nn/img/sad-panda-201402200631.png) 50% 50%;\n background-size: cover;\n height: 100%;\n text-align: center;\n font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, sans-serif;\n }\n table {\n height: 100%;\n width: 100%;\n table-layout: fixed;\n border-collapse: collapse;\n border-spacing: 0;\n border: none;\n }\n h1 {\n font-size: 42px;\n font-weight: 400;\n color: #400090;\n }\n p {\n color: #1A1A1A;\n }\n #message-1 {\n font-weight: bold;\n margin: 0;\n }\n #message-2 {\n display: inline-block;\n *display: inline;\n zoom: 1;\n max-width: 17em;\n _width: 17em;\n }\n </style>\n <script>\n document.write('<img src="//geo.yahoo.com/b?s=1197757129&t='+new Date().getTime()+'&src=aws&err_url='+encodeURIComponent(document.URL)+'&err=%<pssc>&test='+encodeURIComponent('%<{Bucket}cqh[:200]>')+'" width="0px" height="0px"/>');var beacon = new Image();beacon.src="//bcn.fp.yahoo.com/p?s=1197757129&t="+new Date().getTime()+"&src=aws&err_url="+encodeURIComponent(document.URL)+"&err=%<pssc>&test="+encodeURIComponent('%<{Bucket}cqh[:200]>');\n
</script>\n </head>\n <body>\n <!-- status code : 404 -->\n <!-- Not Found on Server -->\n <table>\n <tbody><tr>\n <td>\n <img src="https://s.yimg.com/rz/p/yahoo_frontpage_en-US_s_f_p_205x58_frontpage.png" alt="Yahoo
Logo">\n <h1 style="margin-top:20px;">Will be right back...</h1>\n <p id="message-1">Thank you for your patience.</p>\n <p id="message-2">Our engineers are working quickly to resolve the issue.</p>\n </td>\n </tr>\n </tbody></table>\n </body></html>'
Pls help me getting this error
<p id="message-1">Thank you for your patience.</p>\n <p id="message-2">Our engineers are working quickly to resolve the issue.</p>\n
sure sounds like yahoo's servers are telling you they have an issue and are asking you to be patient
Can you implement dynamic method variations based on the content of a class? Let's say i have a datatype that can hold either a list or dict. and i want different methods available based on that.
def add(self, item):
self.data.append(item)
###
def add(self, key, value):
self.data[key] = value
you'll generally want to avoid such things as it can be confusing, also in Python you can only have one function definition with one name so the second def add would override the name add
you CAN do logic in a function to check the types of arguments, but it's generally considered unpythonic
You technically can, but you really shouldn't
there are some rare cases where you might want to implement logic like that using e.g. instanceof but yeah, avoid it
Even if i agree, i want to try it out here. So how would you make conditional method declarations at init time? Can i just wrap them in an if statement?
You can check a cached bool that tells you whether you're holding a dict, run an isinstance check, or do something else to determine what to do with the arguments. You could also create a baseclass with shared logic and then two subclasses, one of which holds a dict and the other a list
Yeah
why would you make conditional method declarations instead of putting the if in the function?
anyway there's a whole metaprogramming thing with Python that may help you, it gets pretty confusing pretty quickly tho
If you really have to, I'd suggest overriding the method in a subclass, as that would make the code significantly more readable
I think it becomes messy if you handle multiple unique cases in the same function. when you can separate them into their own contexts
yes, so why are you trying to give them the exact same name, but different parameter count, and argument types?
THAT is also very unclear
Implementing function overloads has the unfortunate side-effect of complicating traceback, which would be avoided with method overrides. Other than that, you could just do
def add(self, arg1, arg2=None):
if isinstance(self.data, dict):
self.data[arg1] = arg2
return
self.data.append(arg1)
Sure, but none of this will be exposed to the user. The idea is that i want to offer one type that can handle multiple different data types.
you might want a factory
Since python is dynamically typed, what benefit would there be to using a single type over a factory that gives you different types?
anyway, the most common way I think people would solve something like this is
class ListManager():
def add(self, some, things):
pass
class DictManager():
def add(self, some):
pass
def get_manager(arg):
if isinstance(arg, list):
return ListManager()
if isinstance(arg, dict):
return DictManager()
raise NotImplementedError()
foo = [] or {}
manager = get_manager(foo)
manager.add(foo)
or something along those lines
If you really wanted to, you could override __new__ in a base class, but like... ew
I've done some static methods that iterate through __subclasses__ and return based on some match
It might not be the best idea. But it's worth trying new ideas. I'm used to only using statically typed languages. so the dynamic nature of Python makes for some really interesting possibilities.
well there's dynamic and there's confusing
Having those possibilities doesn't necessarily mean you should abuse them
I don't disagree. but you can't know what is a bad idea until you've tried it.
For example, what would happen if you passed two arguments to a list manager? Would you silently swallow the second argument, or throw an error?
really there's no problem with just adding the type to the function name
add_dict(...)
add_list(...)
clear, well documented, doesn't confuse IDEs, etc.
pathlib.Path cough
You can, however, reflect on previous attempts to implement a similar concept
imo things like that are good left to the rare libraries that really provide sensible functionality with it
Some of the design decisions in pathlib are... interesting
Path concatenation via __truediv__, for example

Strangely, I'm not opposed to that one
I can .. kind of see why someone would think that reduces boilerplate, but it definitely is surprising
But it's uncommon to say the least
the principle of least astonishment is definitely violated with that
but it's .. probably still a good choice
It looks faithful to the path representation at least
on *nix 
And I might be wrong on this one, but IIRC + does the same thing, so at least they don't remove the obvious way to do it?
!e ```py
from pathlib import Path
print(Path('/') + 'home')
@spice pecan :x: Your eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "<string>", line 3, in <module>
003 | TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'
I am indeed wrong on that one
now that pathlib is in stdlib I've gotten pretty used to use it for all kinds of small things that make things easier, like .. reading or writing simple text contents
Since you were curious. I'm making a small helper library for a menu system. On the users end it will look like this.
Any default data type with items that are callable will be allowed.
from menu import Menu
mpd = Menu({
"play/pause": mpd.toggle(),
"play" : mpd.play(),
"stop" : mpd.stop(),
"next" : mpd.next(),
"prev" : mpd.prev()
})
start = Menu([
run("firefox"),
run("pcmanfm"),
])
talk = Menu({
echo("foo"),
echo("bar")
})
Path(MY_CONFIG).read_text() is just quite a bit more convenient than with open() ...
Pathlib on its own is very useful, it's just that certain decisions are not ones you'd expect
I still think subclassing would be a better approach
so what do you do if there are no keys?
it uses the display implementation for the type @hasty swift
right
looks like you'll want to check isinstance in __init__
or something like that
and normalize
I'd then write similar isinstance checks in add if the idea is to add menu items with that, or do something based on argument count
so e.g. range() works so range(3) == range(0, 3) == range(0, 3, 1)
class BaseMenu:
def __init__(self, data):
self.data = data
class ListMenu(BaseMenu):
def add(self, item):
self.data.append(item)
class DictMenu(BaseMenu):
def add(self, key, value):
if key in self.data:
raise KeyError('The specified key is already present')
self.data[key] = value
def Menu(data):
if isinstnace(data, list):
return ListManager(data)
return DictManager(data)
This makes me curious. in order to decrease confusion. Would it possible to override the type association and link the underlying data types methods through the class?
So you could use it as if it weren't wrapped? For example.
test = Menu({
"123": Echo("123"),
})
test["456"] = Echo("456")
you can do various overrides on the magic methods like __setitem__ and __getitem__ or whatever they're called, but it's not exactly going to reduce confusion in this case
This is all pretty interesting. thanks for the input
Since __getitem__ and __setitem__ have the same interface on all classes, you could implement it once and it would work for both dicts and lists
Would you follow camelCase or snake_case notation for API parameters that being used by a language that follows camelCase? E.g. a flask api that being used by Angular
@terse orchid I have thought about the same thing tbh
but afaik REST is supposed to be camelCase
irrelevant of the backend language
Hmm I see, you are right
in general, I as a user prefer to see APIs follow camelCase but thats actually a good question haha
I will continue with the camelCase 🙂
uh any idea where the free help channels are?
im doing some tensorflow and ran into a massive issue that is "fixed" tahts what the git says 🤣
For me it's between lobby and discussion categories, if I understood you correctly
#help-potato for example
It depends on how high-level your library is. If it's a 1:1 mapping with the API, use whatever the API uses. If it's a bit more abstracted and/or specifically meant to be "pythonic", then use snake case
hello
@frigid mortar so are you making your own language, or interfacing with other languages?
Recreating Python (and various other languages) API within the D programming language.
It sounds like you intend to recreate the Python standard library, as well. Reimplementing enough of the language that the standard library will work is a huge amount of work.
theres certain things python does that i would like to see in other languages, but a lot of the things are going to be so hard to do that you would have to basically create another language within the language.
something along the lines of doing this:
module App;
import Python;
void main(string[] args)
{
print("Hello World!");
}
Where generally by importing Python u can start accessing functions (by name/space and casing) as youd expect to find them from the base lang.
are you planning on linking against an existing Python interpreter, or creating your own?
hm, this would only allow access to python builtin functions? the smallest part of python that i like is the functions, a lot of it is the syntax for which you need a interpreter.
The most direct way to describe it is:
Its about reducing the amount of actual learning of another language to be productive.
how would you recreate things like comprehensions (assuming they are not in the language already)?
what about dunder methods? are there equivalents to these concepts that you can translate to in D?
got an example of what those are?
!listcomp
Do you ever find yourself writing something like this?
>>> squares = []
>>> for n in range(5):
... squares.append(n ** 2)
[0, 1, 4, 9, 16]
Using list comprehensions can make this both shorter and more readable. As a list comprehension, the same code would look like this:
>>> [n ** 2 for n in range(5)]
[0, 1, 4, 9, 16]
List comprehensions also get an if statement:
>>> [n ** 2 for n in range(5) if n % 2 == 0]
[0, 4, 16]
For more info, see this pythonforbeginners.com post.
im unfamiliar with that concept specifcially o.o
!e ```py
class A:
def init(self, num):
self.num = num
def add(self, other):
return A(self.num + other.num)
def repr(self):
return f"<A {self.num}>"
one = A(1)
two = A(2)
print(one + two)
@finite sparrow :white_check_mark: Your eval job has completed with return code 0.
<A 3>
as you can see this modifies the way addition works by defining a special method in a class.
this is the way you do operator overloading in python. you can also observer __repr__ which is the string representation of a class
dlang has operator overloading 😛
but there's the reversed addition operator __radd__, which isn't a concept that exists in most languages.
and accourding to some page i was looking at we got some weird crap with array crafting o.o
had no idea that existed
the core issue remains: how will you replicate syntax specific to python in a entirely different language?
i cant exactly replicate 'syntax'... but its not exactly my goal to either.
hell, D is statically typed right? how will you deal with how python handles types?
the only thing that would make something like this worthwile, imo, is if i could use python syntax.
!e ```py
class A:
def init(self, val):
self.a_val = val
def add(self, other):
return NotImplemented
class B:
def init(self, val):
self.b_val = val
def radd(self, other):
return B(self.b_val + other.a_val)
print((A(5) + B(10)).b_val)
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
15
the fact ur bot will print results is amazing o.o
note here that we've got an A on the LHS and a B on the RHS of a +. The A says "I don't know how to add", then the B gets a chance to define how to add.
it relies on a tool called snekbox which does safe code evaluation using nsjail. you might want to check whether the Piston code execution engine can do D, it does a similar thing for a lot of languages.
B.__radd__ is called if and only if A.__add__ returns NotImplemented. That's different from how operator overloading works in a language like D, where I believe the appropriate equality comparison method is resolved statically at compile time.
you wouldnt see a benefit even for the purpose of just moving something low level for more power?
nope. if i wanted more power i would go to cython. which is a statically typed compiler for python syntax that compiles to c++.
i had no idea that existed o.o
If D can produce shared libraries with a C ABI, then you can write Python modules in D.
thought so.
the main thing that would make me switch (more like, learn another) language is not that the other language replicates python, i already have python for that after all. its that the other language can offer me something else that i value enough to learn the language.
ic
for example i learned a bit of HLSL because it provides me with something VASTLY different from python, mainly highly parallelised computation on the GPU
something else that could make me learn another language is easy native (not electron) cross platform GUIs
What if there were tools in the language you may want to interface with but dont have good compatibliity with python directly?
FWIW, this is the normal way of writing high performance Python modules. numpy is written largely in C, and links against Fortran libraries, for instance.
was about to be the type of thing i was asking on xD
another language doesn't need to be compatible with python. HLSL is basically impossible to get to work with python, hence i learned a bit of C# to be able to even run the HLSL. and by learned i mean that i googled syntax for a bit because at a certain point the only thing you need to look up is syntax, which is not a hard thing to do.
if i were to learn another langauge, i would want something vastly different from python. if i can write python extensions in it, neat, but thats not a requirement by far. the language would be one that can be used on its own.
if i were to learn another langauge, i would want something vastly different from python.
+1 on this. In my daily life I regularly switch between C++, Python, and shell - languages are more useful to me if they satisfy a radically different niche than one of the ones I'm already comfortable in.
If it's doing something I can already do in one of those languages, I'd just use that language.
maybe im just used to crappy devs then xD I started the multiple projects i have because the communities i grew up refused to even try new stuff if it meant starting from 0... having the functions exist was a compromise they said at least made it viable cause they could immediately use syntax they were learning in a useful way rather than experiment for weeks before knowing how to do anything specificly.
i hope that helps you understand why i think the whole idea of porting another languages functions over is not really great. beacause it often doesnt matter. why do i care that my way of printing out is println!("hey") rather than print("hey")? why does it matter that i have to import java.lang.Math; instead of import math? in the end those are small syntax differences, which dont matter when compared to the underlying structures. i dont like java because of its approach to OOP, which is not something someone can fix by making a library of functions that have the same name as in python. i have no opinion on D, this is just a example to show what i really consider important.
and moreover: if you advertise "Python compatible" as a feature, you'll be forever fixing bugs that you don't care about at all because people find things that aren't actually Python-compatible.
i think this is a opinion often held by beginners, which have the tendency to at some point not be beginners anymore.
If people were actually using it and complaining id be pretty happy to fix it, itd give me something to do thatd id know at that point IS being utilized
Python is a weird language. Names aren't resolved when the file is compiled, or even the point where the module is imported, but at the point where the function is called.
You'll find implementing your own Python-compatible language isn't trivial. It works very differently than most languages.
I wouldnt have concidered trying if Dlang werent also fekin weird
you seem to like D, Awsome!
make projects in D, and if you want to work with people that are unwilling to use D why not let your part of a project and their part of a project communicate over a language agnostic protocol (API, message queue, etc) so that they can use whatever they want.
it has some features inspired heavily by scripting langs
!e ```py
def to_string(i):
return str(i)
print(to_string(42))
str = print
to_string(100)
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
001 | 42
002 | 100
thats awful
maybe, but things like that are used all the time by unit tests.
!e ```py
def n_adder(n):
def add_n(x):
return x + n
return add_n
add5 = n_adder(5)
print(add5(add5(2)))
@raven ridge :white_check_mark: Your eval job has completed with return code 0.
12

I mean, don't do that. But mocks do that.
that is a very weird function o.o
it servers to illustrate a concept, not as good code
for a func inside a func?
that's a closure. I'm sure D has them, but it's an example of a way to capture variables from outer scopes in Python.
ah
Which also serves to demonstrate some of Python's name lookup weirdness.
that is spooky either way xD
the way Python looks up names is very different than how most other languages do it. Most languages resolve a name to a memory address at compile time, Python does it each time the function is called.
makes me wonder, is there a way to force python to bypass this, to improve performance?
Cython has an option for that.
yeh well, thats c++ in the end not python
right. Python doesn't have a way to do that.
hmm, i'll have to take a look at the bytecode, maybe i can make something truly awful out of this idea
if you want to get ugly, there is a tricky way to early bind: default arguments for a function are bound early.
it used to be advised as a Python performance hack
it's still used in some places in the stdlib.
is that also responsible for the mutable defaults problem?
example?
Lib/sre_compile.py lines 411 to 414
def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
s = bits.translate(_BITS_TRANS)[::-1]
return [_int(s[i - _CODEBITS: i], 2)
for i in range(len(s), 0, -_CODEBITS)]```
why does that improve performance?
OHHH ive used that before, in my thing where i tried to replicate classes purley with decorators.
python looks names up in the locals scope, then the enclosing scope(s), then the global scope, then the builtins scope. This makes it so that a lookup of _int against the locals can find int, saving the need for a failing lookup against locals followed by a failing lookup against the enclosing scope followed by a successful lookup against the globals
mm, got it
but the general consensus these days is that you shouldn't micro-optimize Python code like that - if it's performance sensitive enough to justify that microoptimization, it probably should be written in C instead.
Haha, sometimes you forget about the overhead of iterating on the python end, and sometimes you get a 30fold performance increase by passing a numpy array into a c function instead
my favourite
i assume you know the Cursed stuff that @CEO#8158 made in #esoteric-python
I need some help with n-gram model in ML
Given the headline of the news, the objective is to identify the category of news.
Note: Use the only headline as input and predict the
News_Category_Dataset.json
category
For example:
Short_description: ....
Headline: There Were 2 Mass Shootings In Texas Last Week, But Only 1 On TV
Date : ....
Link : ....
Authors: ....
Category: CRIME
Consider the following categories only: Business, Comedy, Sports, Crime, and Religion
The Features of this dataset is described as:
a. Lexical Features: Word n-gram.
b. Syntactic Features: Parts of speech tag unigrams.
Implement n-gram (n=1,2 and 3), called unigram, bigram and trigram features for each instance. You may choose only the most frequent n-gram to provide as a features for your model. For n=1, use 500 most frequent unigram, similarly use 300 and 200 most frequent bigram and trigram respectively.
For parts of speech tag unigrams, first you need to get POS tag for each instance. You can use any library like Stanford POS tagger. Find the following link for it:
https://nlp.stanford.edu/software/tagger.shtml
For NLTK POS tagger see
http://www.nltk.org/book/ch05.html
Similar to lexical feature use 500 most frequent unigram to build the model.
Result and Evaluation: Perform 10-fold cross-validation and report the performance of the classification models: Decision Tree, SVM, ANN and Maximum Entropy. Analysis the result with different performance evaluation metrics.
Dataset: news-category-dataset.json
Hey @craggy birch!
Uh-oh! It looks like your message got zapped by our spam filter. We currently don't allow .json 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:
consider posting this in a help channel ( #❓|how-to-get-help ) or in #data-science-and-ml instead, this isn't really on topic for this channel
okay thanks
You guys should hide this channel somewhere people won't be able to look up easily, lol
When writing a __eq__ method for a class, does it make sense to use a -> bool type hint for the return? If not, why not?
strictly speaking, the return type is bool | NotImplemented
but if you never return that, it should be fine
Pre-3.9, would that be typing.Union[bool, NotImplemented]?
Where did you read that?
it seems to something of a mess
If I don't return NotImplemented (and that issue wasn't there), would it still be correct to use -> bool | NotImplemented? If so, why?
yes
you can return NotImplemented from almost any dunder to notify python that you don't know how to handle the type you were given
ohh
and python will try to ask the other type, and if that fails, it will error out
it is pretty important to do so as often as possible, since it makes it easier to integrate your types into other codebases
Why do you return NotImplemented instead of raising NotImplementedError?
they behave slightly differently
What behavior does returning NotImplemented offer that is desirable over raising NotImplementedError?
!e
class A:
def __eq__(self, other):
if isinstance(other, A):
return True
raise NotImplementedError()
class B:
def __eq__(self, other):
if isinstance(other, B):
return True
return NotImplemented
class C:
def __eq__(self, other): return True
print(f'{B() == C() = }')
print(f'{A() == C() = }')
@flat gazelle :x: Your eval job has completed with return code 1.
001 | B() == C() = True
002 | Traceback (most recent call last):
003 | File "<string>", line 15, in <module>
004 | File "<string>", line 5, in __eq__
005 | NotImplementedError
NotImplementedError will not ask other types
NotImplementedError is mainly used on method of abstract base classes?
In the first print statement, why do both B's and C's __eq__ methods get called. I expected that only one of their methods would get called.
>>> class B:
def __eq__(self, other):
print("B's __eq__ called")
if isinstance(other, B):
return True
return NotImplemented
>>> class C:
def __eq__(self, other):
print("C's __eq__ called")
return True
>>> print(f'{B() == C() = }')
B's __eq__ called
C's __eq__ called
B() == C() = True
it asks B() if it is equal to C(), B() answers I don't know = NotImplemented, so python asks C() instead, which answers yes=True
how to select foreign key by session in django
Hey all
im working on a project where i need a relationship between my two objects
sth like the django foreignkey relationship
where one object can access the stuff of another by just like --> object1.name_of_object2
or if thats not possible, how could i communicate between my module
like django signals where you can execute functions using the signals like post login and etc
how can i go about creating either one of these
I’m not sure if it does actually
because bool has no subtypes and __eq__ is inherited
well not that it wouldn’t make sense
more…redundant?
__eq__s don't always return bools though
numpy arrays for example
In [134]: np.array([1,2,3]) == np.array([1,2,4])
Out[134]: array([ True, True, False])
At least mypy has a special case for NotImplemented - it's OK with you returning it from __eq__ despite declaring the return type as bool
Looks like it allows returning NotImplemented everywhere, and there's an open issue for restricting it to only be accepted in the contexts where the interpreter expects it. https://github.com/python/mypy/issues/363
dp = [0]*n
is there a point in using a dictionary ?
dp = {0: x for x in range(n)}
is it faster?
for dynamic programming
i think your comprehension is inverted? they all have key 0
I'd imagine a list would be faster if you just need an incrementing index
Inserting into a dictionary and appending to a list are both amortized O(1), meaning the performance difference between the two will be at most a constant factor. I expect the list to be faster overall, too.
ah yes I'm sorry
is the dp solution to fibonacci or similar problems equivalent in space and time complexity to the memoization solution?
Yes. Though that's off topic for this channel, and belongs in #algos-and-data-structs
which technically violates LSP I guess? but it’s really ergonomic
you rarely need to call __eq__ directly though, and you aren't going to get NotImplemented back from a == b even if both of their __eq__ return that.
wrt numpy arrays though, I guess it does violate it a bit, since to check for actual equality you'd have to do np.all(a == b), although numpy at least properly raises an exception if you try to bool(arr) and tells you how to get the behavior you want. a matter of practicality and readability for users I guess
yes, but we require that you use english in this server, sorry
Ok, thank you for this information. I am going to using English. 
You all know some lib which can be used for gif compression, have tried using PIL but the rendering is really bad 😔 😔
Have even tried wand, the rendering is good in this case but it's like super super slow and uses too much resources
Can a type gain context over it's containment by traversing backwards?
class X(object):
def y():
# calling A.b() <super.method()>
class A(object):
def __init__(self, menu):
self.x = X()
def b():
pass
To simplify the question. Can X figure out that's being stored inside of A?
Is there documentation on object anywhere? I just recently learned about __subclass__ which captures all direct subclasses of a class that inherit from object and I had no idea of its existence.
I don't think so, at least not without ugly hacks. You can pass in the parent to the object you create
That's true. That will have to do.
You could use the descriptor protocol I guess
But that would only work when accessing it via the holder
Well
So with the descriptor protocol you can capture type in __set_name__ but you don't know the instance until a __get__, __set__, or __delete__ access occur from the instance.
Maybe you could figure it out someway by capturing instances with __new__
With the way you are doing right now however you can never know
unless you like pass self to that X initialization
Guys, what is the most hot niche of data science project or topics in freelance
guys
this is going here cause i have a massive problem
/bug
check out this bit of code
l = {msg: lambda: print(msg.upper()) for msg in ['a', 'b', 'c']}
while True:
i = input()
f = l[i]
f()
you would think that it would print out any letter you give it as input, in uppercase form
but no
it always prints C
this is a simplified version of a problem i have in one of my projects
doing it without a list (or dict) comprehension works of course
if i manually did a: lambda(print('A')) and so on for each entry in the dict
but can anyone tell me why this doesn't
and so you guys don't have to waste time figuring out the code, the dict it gives looks like this
{
'a': <lambda that should print 'A' but prints 'C'>,
'b': <lambda that should print 'B' but prints 'C'>,
'c': <lambda that prints 'C'>
}
``` and it's indexed with the user input over and over
weirdest shit i've ever seen
unless i'm missing something huge
just closure behaviour i guess
Values outside of function's scopes don't persist, try using functools.partial instead
l = {msg: partial(print, msg.upper) for msg in ['a', 'b', 'c']}
the lambda captures the msg variable from the outer scope but msg's value changes until it reaches c
Because that's the last value used
!d functools.partial
functools.partial(func, /, *args, **keywords)```
Return a new [partial object](https://docs.python.org/3/library/functools.html#partial-objects) which when called will behave like *func* called with the positional arguments *args* and keyword arguments *keywords*. If more arguments are supplied to the call, they are appended to *args*. If additional keyword arguments are supplied, they extend and override *keywords*. Roughly equivalent to:
```py
def partial(func, /, *args, **keywords):
def newfunc(*fargs, **fkeywords):
newkeywords = {**keywords, **fkeywords}
return func(*args, *fargs, **newkeywords)
newfunc.func = func
newfunc.args = args
newfunc.keywords = keywords
return newfunc
It basically freezes some arguments for your function
alternatively you can shadow the variable in the lambda inputs
l = {msg: lambda msg=msg: print(msg.upper()) for msg in ['a', 'b', 'c']}
``` note the msg=msg
oh makes sense
Yeah, using default arguments forces active evaluation
alternatively if the arg is a string can i just do str(msg) inside the lambda somewhere to create a new string from it and not use a reference copy that way?
does the string constructor work like that
the partial thing is good but not the most readable
Not really, the issue here is that msg is not looked up until the dictionary is created
oh ok
The lambda is not aware of the value msg had at the point of its creation, all it knows is that it's supposed to look that name up and use whatever it gets in return
No idea which chat this question would fit in, but if I have a dict of products by id, and another dict of data pertaining to those products(quantity in cart, chosen warranties, etc.), would that second dict be considered "metadata" for the first dict?
Technically it's data about the data, which would make it metadata. However, I personally wouldn't use it because when I hear metadata I think of associated data to a file.
this question is probably fine for python general
Also that
So the other day we had a really interesting discussion about propagating nulls throughout code which I've tried to adopt into my own code, but I was curious as to how people handled propagating nulls when you have functions with multiple returns. For example:
return_1, return_2 = null_func()
Will throw an error because you're trying to unpack None. It gets pretty awkward to handle this because you have to do something like:
func_returns = null_func()
if func_returns:
return_1, return_2 = func_returns
It would be nice to handle this at a decorator level instead of doing the above for every function, but not sure if that's possible.
At that point you might just want to consider returning a dataclass/named tuple instead
In a task of a job application the gave me some instructions. In the document, this is being told.
They asking for a flask app. Do you think they want me to implement blueprints or not? I barely need two api functions.
"you can implement direct api functions" or "you definitely should implement a blueprint with flask.Blueprint"?
So, as an example, suppose you have a function that simultaneously computes mean and stddev, you use it like pop_mean, pop_stddev = compute(), with a dataclass you'd just do pop_stats = compute() or something like that, and then just use pop_stats.mean and pop_stats.stddev. It even has advantages over multiple returns, with multiple returns it's up to the user to get the order correct when unpacking, and it's easy to make a mistake
those kinds of mistakes cannot happen with a dataclass
obviously you have to check for null either way, but with the dataclass version you never plan to unpack anyway, so you don't need to do this silly two step dance where you first assign to a single variable, then check null, and then unpack
but pop_stats = compute(), when being given a None, still would be none.
Yes, but you don't need to unpack after
ah, i see what you mean
Either way, if you get an Optional, you have to check for None
I thought you were just unhappy about having to first assign, and then unpack, when all you want to do is unpack
I've felt the same way before
Another nice thing about dataclasses vs returning a simple tuple particularly when you have optionals, is that with optional and tuple the type is already starting to become annoying to read
Optional[Tuple[float, float]] in my example above, and that's the simplest case
and it can keep getting worse, Optional[Tuple[float, bool, List[Foo]]] etc and things become unreadable so fast
This is obviously a general issue but it's just exacerbated here by the Optional
This is something I've toyed around with, actually. Considering making all the function returns in our libraries be uniform. Dancing back and forth between tuples and dicts
Is there documentation on object anywhere? I just recently learned about __subclass__ which captures all direct subclasses of a class that inherit from object and I had no idea of its existence.
tuples are kinda sub-optimal because the things don't hav enames, with dicts there are names but nothing helps you check you got the names right
named tuples avoid both of those issues. I'd personally just use a dataclass, but you can use a named tuple as well
attrs supports slots, which would eliminate/mitigate one of the downsides of dataclass compared to tuple
namedtuples are great --- they have mostly the same footprint as a tuple
I'd be curious now to measure the memory usage of a named tuple, vs a tuple, vs a slots attr class, vs a regular dataclass (no slots)
Tuple VS namedtuple has no difference IIRC
Yeah they work through properties that access the tuple that's actually holding the data, technically there's some cost to the type itself but the biggest disadvantage is that the namedtuple type is somewhat costly to create (and its simplicity if it's not desired)
it seems like slots is actually very efficient, closer to a tuple/namedtuple than a regular class
collections.namedtuple also kind of sucks, you define them using string literals which is kinda eww
it's nice to have the option for fancy programmatic stuff
but for simple things feels pretty silly
it does accept a list of field names, but I prefer NamedTuple if it makes sense to use it
namedtuples also don't have type annotations if you don't use typing.NamedTuple, but I suspect that NamedTuple is basically identical to attrs with slots
except much more limited. So at that point, I don't really see a reason to use it ever.
it uses namedtuple from collections internally for the actual type, I find it very useful to group information
Sure, you're declaring a class though, idk, if I knew how to measure it rigorously I would, but from what I can see e.g. from the linked post, I think slots implementations are going to have very similar per-object memory usage
attrs will let you group information just as well, and it's a lot less brittle, e.g. if you decide you want to allow mutation, you just unset frozen, with NamedTuple you have to change to something else
a namedtuple gets you some behaviour for free like the repr and a couple of methods (that you'll probably never use) compared to a class with slots but I'd expect the memory usage to be a bit higher. dataclasses, attrs etc. feel a bit overkill to me if all you're using it is to group and get named access to a structure you return for example
My most recent use of it was when I needed to accept/return a group and a name as an identifier but the order they should be in wasn't exactly clear, so I just used the namedtuple to get rid of ordered access
namedtuple doesn't get rid of ordered access, that's part of the thing
it does get rid of unpacking though
sorry, my mistake, it gets rid of nothing
it's just a superset of tuple behavior
Yeah it's still there to be used for unpacking or something but the main interface moves from having to order the paremeters in some way to just accessing the names from the object
dataclass doesn't support slots, so if you care enough about slots, I can see how in very specific contexts adding a dependency on attrs would be "overkill"
but, realistically if you already have attrs as a dependency, or if dataclass supported slots, I don't see what's overkill about it
named tuples actually generate more dunder methods etc, by default, since they're hashable, inequality comparable, iterable, indexable, "sized" (length works on them)
looks like NamedTuple is implemented using slots as well (that's what I suspected)
The only thing with slots it should be doing is setting them to be empty
yeah maybe you're right, I didn't see slots in the source, not sure what that comment was on about (I saw a github comment with someone saying it uses slots)
faster than attrs and dataclasses
It really just dynamically creates a type with tuple as its parent with some methods attached to it, including properties which fetch the values from the tuple it subclasses. So there's no need to store any instance data
Meh, really depends what you're doing, I'd actually prefer things to be initialized at startup than lazily
Also his "fireball" story isn't that compelling, attrs already solves this with kw_only init, which dataclasses is getting soon, and IMHO it should be the default anyway for most of your dataclasses/attrs
Really, at the end of the day, attrs, dataclasses, and cluegen, are all insane, even if python is dynamic, it should support a way to declare a class with fixed fields, of known type, first class, in the language, nicely, with reasonable perf 🤷♂️
why add a dependency with 1000s of lines of code when you can just drop in a 100 lines
why do you think the number of lines of code in a dependency matters?
debugging
that's cause there's too much code to go through, you'd follow if there was less code
it's you that I don't follow, not the code 🙂
i'm just following the code
I've never needed to debug the internals of dataclasses or attrs, ever...
they do what it says on the tin
i've never needed attrs
more important than LOC of your dependency is how well supported it is, how many other people use it (and are running into bugs and reporting them, before you ever do), etc
I mean you needed functionality similar to it, i presume, otherwise why mention cluegen.
i mentioned it because no one else did
A: If you're using it, you do. You maintain cluegen.
the best
idk, it's incredibly common to need something like cluegen/attrs/dataclasses/pydantic, unless you like to work with unstructured data exclusively, which I guess some people do
maybe it would just be simpler to fork python, add first-class dataclass functionality, and maintain it myself 🤔
How do you go about making an enum with multiple values? Using namedtuple as the value for the enum?
I also seem to remember finding a way to subclass the Enum class itself, but cannot find the link to that
If you Google python enum, you should probably get a hit inside the official docs
Or search the docs directly
Not exactly sure what you mean by multiple values for the enum
But yeah the enum values can be anything, including tuples
A frozen dataclass would be good
I mean like if I want
class MyEnumValue(NamedTuple):
name: str
value: Any
class MyEnum(Enum):
SOME_VALUE = MyEnumType('name', 'value')
But, come to think of it, I don't need enum in my case
Yeah you can do that
Looks like you can either use aenum to do it with MultiValueEnum or you can hack enum (https://stackoverflow.com/a/43210118)
Not sure if you could use a named tuple with it or not tho
wouldn't that lead to duplicate attribute usuage (e.g. MyEnum.SOME_VALUE.value.value and MyEnum.SOME_VALUE.value.name)?
I ended up using data class since I don't really need to convert arbitrary value to enum
Is the only way to get class attribute is by using PEP 520?
I'm kind of confused how you ended up using dataclass instead of enum, they do very different things, so I don't really understand
I just figured I don't need enum to hold my namedtuples/dataclasses
okay
by "get class attributes", you mean, you want to iterate/reflect over the attributes of a class?
Yeps, but I want to get them in the order they are declared
That's why I reckon I don't need enum
the best way to do that if I understand you correctly is to use fields
err, why do you reckon that?
these two things, again, have nothing to do with each other
Because I ended up putting my dataclasses inside a class and process them declaratively? That's why I need to fetch them in the order they are declared.
I was going to treat the dataclass as enum values but rekon it won't be necessary since I can just use the reference as some kind of enum anyway
you put your dataclasses in a class? That's... weird. Why not put them in a module like normal?
not sure I understand, but if you need them in order, why not put them in a list?
Why doesn’t python have binary search tree?
maybe some 3rd package supports bst?
@sand goblet there is the bisect module which provides the core logic
Well, for array backed binary search trees
And well, you are generally supposed to use a set/dict
I am majorly confused about logging
why does this print 1
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger.info(1)
but this does nothing
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info(1)
hello, i don't know why it doesn't work. Plz help me.
