« first day (3494 days earlier)      last day (1459 days later) » 

6:16 AM
Hi
 
7:14 AM
@aderchox If you expect only tuples up to length 4, that type hint is misleading. A union of tuples from length 0 to 4 would be more appropriate.
@smci I usually isolate the code in question and do a whole-program test (RSS over time). Gauging the size of individual objects has certain pitfalls that re not solved by more accurate reporting tools. I've seen pympler recommended a few times, though.
@AndrasDeak I sometimes use tuples as a "frozen list". Not a perfect match, though.
 
 
1 hour later…
8:25 AM
@MisterMiyagi \0\0
 
 
1 hour later…
9:28 AM
Good morning guys, i have some issues trying to import one file to another, and the other way around
https://pastebin.com/0hhtgFJz

i basically want to divide my 3k lines long program to smaller segments, so it's more manageable
but even though it looks i have imported the files correctly, i still get errors like this :

File "C:/8W_Control_10G/MTS5800.py", line 17, in <module>
import GUI
File "C:\8W_Control_10G\GUI.py", line 17, in <module>
import MTS5800
File "C:\8W_Control_10G\MTS5800.py", line 33, in <module>
GUI.warning_traffic_not_connected()
AttributeError: module 'GUI' has no attribute 'warning_traffic_not_connected'
 
Generally you should try to avoid circular imports
although there's probably still a way to make it work if you have a if __name__ == '__main__': guard
 
so i should import all the "segments" to the GUI file?
 
yep, that sounds like a better design
 
Uh the thing is, i make GUI calls to all the tests, you know to update the GUI so the user what is pass or fail
 
Ok, so it depends on what kind of program you're writing. If possible, you want the GUI to control what happens and display the results. (In other words: The GUI uses the other code to achieve what it wants, but the other code doesn't need to know anything about the GUI)
If you really really need the code to interact with the user, then you should create an interface that allows it to do that. For example, you could create a GUI class and pass an instance of that class into your mts5800_main_tests function
In short, instead of importing a GUI module, you should pass in a GUI object as an argument
 
9:41 AM
The program is written, it's just a one file 3k lines monstrosity :P
I have the if __name__ == '__main__': in the GUI file , but i still get the same error, so apparently i have to use a class, since i need it to interact with the user in some tests
Thank you for taking time to answer me!
 
 
2 hours later…
11:59 AM
hey guys...new to python.... how do I find replace in a list in python?
I tried this code:
def update(arr,find,replace):
	for i in range(len(arr)):
		if arr[i]==find:
			arr[i]=replace
	return arr
But it seems, it ain't working
 
Looks correct to me
 
And google simply gives out ways I can't even comprehend (As I said 3rd day in python).
array = update(array,x,y)
Is this call fine?
 
Yes, but I don't think it's a good idea to both modify the input array and return it
If you return something, it should usually be a new object
Decide if you want update to create a new array or modify the input array
 
Um.. what is the default type of passing in python? i.e., pass by value or pass by reference?
 
python can only pass by reference
 
12:02 PM
@Aran-Fey Modify the input arra
*array
 
Ok, then it's good practice to not return anything
 
@Aran-Fey Oh okay. I didn't know that
 
Like the builtin list functions do (append, extend, sort, etc)
 
def update(arr,find,replace):
	for i in range(len(arr)):
		if arr[i]==find:
			arr[i]=replace
This much should be fine you mean?
 
yeah, like that
 
12:03 PM
@d4rk4ng31 Please see this to see how names and mutation work in python
 
@AndrasDeak Thanks man!!! That doc was awesome :)
 
yup, no problem
 
12:18 PM
def augment_twice_bad(a_list, val):
    """Put `val` on the end of `a_list` twice."""
    a_list = a_list + [val, val]

nums = [1, 2, 3]
augment_twice_bad(nums, 4)
print(nums)         # [1, 2, 3]
Can you explain this example?
As in why the list doesn't get modified?
a_list = a_list + [val, val]
In this statement, we are changing a_list right?
Then why should the fact that val is a local variable matter?
@AndrasDeak?
 
a_list + [val, val] creates a new list, and a_list = gives that list the name a_list. But this name is local to the function. You never modify the input list.
names and values are two different things
 
Oh yeah!! right. But then had I done a_list.append([val,val]), would it have worked?
 
Yes, because that modifies the list instead of creating a new list
 
Ah...I get it. Thanks :)
 
actually you'd want to use extend instead of append
 
12:24 PM
You're getting it! This is a common stumbling block for new Pythoners.
 
Now that I think of it, It is just like the x+y example in the link shared by andreas.
 
Andras, not Andreas. But yeah. It needs some getting used to but it's actually pretty simple. += not withstanding...
 
Naz
12:39 PM
Why does mypy say that this function returns Any and not None?
def findTreasure(row: int, col: int, grid: List[List[str]]) -> None:
    nonlocal m, n
    grid[row][col] = "x"
    # for each landpiece, attempt to go left, right, up or down if there is land
    if land_down(row, col):  sail_down(row, col)
    if land_up(row, col):    sail_up(row, col)
    if land_right(row, col): sail_right(row, col)
    if land_left(row, col):  sail_left(row, col)
    return
sail_down:  Traverser = lambda row, col: findTreasure(row + 1, col, treasureMap)
sail_up:    Traverser = lambda row, col: findTreasure(row - 1, col, treasureMap)
sail_right: Traverser = lambda row, col: findTreasure(row, col + 1, treasureMap)
sail_left:  Traverser = lambda row, col: findTreasure(row, col - 1, treasureMap)
Row = int
Col = int

Traverser = Callable[[Row, Col], None]
 
What if you just remove the (unnecessary) return statement?
 
Naz
tried that
those warnings are in the definitions of sail_*
it underlines the findTreasure part of the lambda expression
number_of_islands.findTreasure(col: int, grid: List[List[str]]) -> None
Returning Any from function declared to return "None"
Returning Any from function declared to return "None"mypy(error)
 
That's not the same function.
The function you posted is findTreasure(row: int, col: int, grid: List[List[str]]) and nested inside another function
 
Naz
ahh
it is the decorator
yeah
done ;)
thanks Aran :)
 
If grid is of any non-trivial size, you will probably bump up against the recursion limit
I did something similar once to do a brute-force color fill and had to implement my own stack, which would easily grow to 50,000 or more deep, way in excess of the Python default limit of 1000.
 
Naz
12:52 PM
Oooh very interesting. So you simply put the calls that you want to make in the stack? Do you have an example of that code? Sounds very interesting. This is a leetcode problem called "number of islands". I did a Pirate's of the Caribbean themed solution there lol
And it passed, so looks like they did not have very large grids
 
Typed code pains me, apparently
At least typed lambdas...
 
Naz
is there actually any point in typing lambdas
 
very uncommon, because typing requires assignment, and lambdas shouldn't be assigned
 
Naz
Yeah, but it reads nicer, in comparison to writing out a full blown function there. And if I were to just write out the logic completely to sail and check land without the lambdas or full blown functions it would read worse. But I agree that this is a bit of a gimmicky solution
 
I don't see why it would be worse to have full if blocks (which are recommended anyway) with the code copied over from each lambda
if land_down(row, col):
    findTreasure(row + 1, col, treasureMap)
if land_up(row, col):
    findTreasure(row - 1, col, treasureMap)
if land_right(row, col):
    findTreasure(row, col + 1, treasureMap)
if land_left(row, col):
    findTreasure(row, col - 1, treasureMap)
Plus it's a bit weird to me that "if land down, sail down".
 
1:31 PM
@Naz No, I only kept the grid locations on the stack, and then pushed/popped it as I walked the grid. Nowadays I would use a deque.
 
 
3 hours later…
4:02 PM
@MisterMiyagi Ok. This is not production code, it's some ML stuff I'm working on, and I want the specific memory usage of a couple of specific data structures (which may be using C extensions).
 
4:45 PM
@Arne I can see use though... if I've got lambda x: # whatever, I can see utility/clarity in typing lambda x: float: # something that an int won't do forbut annotations aren't supported for lambdas?
 
I think we were talking about typing the return value
oh nevermind, you can't even type the args
 
wim
5:17 PM
@smci Hmm, yes I guess you're right about SEO
 
@toonarmycaptain The argument type should be fixed through the context in which the lambda is used. Or rather, the typing should happen on the consumer side of the function. I didn't know it isn't supported, but I also never tried
 
@toonarmycaptain just use a single-line def, e.g. def sail_down(row: int, col: int): findTreasure(row + 1, col, treasureMap) if you care about length
a lambda only has downsides if you assign it to a name anyways.
@smci depending on what level of detail you need, using resource.getrusage before and after creating the data structure might be suitable
 
6:01 PM
@MisterMiyagi Thanks, I guess a fallback is to wrap that subtraction, e.g. with a decorator @measure_delta_resource_usage on the declaration of interest. But ideally I simply want one direct function call. Has noone here used pympler or can comment on it?
@MisterMiyagi Err, what am I doing wrong here:
>>> resource.getrusage(resource.RUSAGE_CHILDREN).ru_idrss
0
>>> resource.getrusage(resource.RUSAGE_CHILDREN).ru_ixrss
0
Also, AttributeError: module 'resource' has no attribute 'RUSAGE_BOTH' contrary to documentation, so resource.getrusage(resource.RUSAGE_BOTH) doesn't work.
^ answer on RUSAGE_BOTH : I guess it's not available on MacOS (why, it's Linux?)
 
macos is not linux if that's what you meant
 
@AndrasDeak Fine, whatever, the question was why is resource.RUSAGE_BOTH not available on MacOS, and how to work around that? (I don't even think resource.getrusage is the best tool for what I want). And more generally have you used pympler or any other decent memory reporting on a specific data structure that may use C extension.
 
6:17 PM
I'm just saying it's not "contrary to documentation", because the doc says "May not be available on all systems."
Side note: star import in the examples, ew
in any case my linux reports 0s for your values and also .ru_idsrss
same with RUSAGE_SELF
 
@AndrasDeak Ok, this is not the issue I originally asked about. I asked "what's the simplest way to get accurate memory reporting on a specific data structure that may use C extension." (Not on an entre process). I seriously don't think resource.getrusageis the answer; have you or anyone here used pympler or any other package you'd actually recommend? (some of the data structures in question are fastai.text.data.TextDataBunch, TextList, TextDataset)
 
 
2 hours later…
Sam
8:47 PM
Heya guys
Is there a way to see how many elements there are with a beautifulsoup 4 css selector?
For example, there are 4 <tr>'s, what would I need to do to get 4 as a result
 
Do you know how to get a list or an iterator over a given css selector?
 
Sam
Nevermind, just got it working, thanks anyway
Thought i'd do some good to humanity and start writing some open-source software
 
Sam
9:15 PM
Can you guys check it out? It seems to work but I keep getting 302's from the server
 
@Sam description is spelled with an "e" (both the code and the readme)
if you have a license for it you can probably use a LICENSE file (?) which would also let github denote the license
And your whitespace is weird...are you using tabs?
and the parentheses in if (condition): are superfluous
(int(port) > 65535) and (int(port) < 0) <-- unlikely scenario
I can't comment on the actual network stuff, or whether it might be a problem to hit that website for the query
 
Sam
9:36 PM
@AndrasDeak sublime text automatically does this
@AndrasDeak whats superfluous?
Thanks for the feedback man, appreciate it
@AndrasDeak Yeah sorry I always use these otherwise I do it without parentheses in java and c# its easier for my brain this way
 
@Sam please don't use such expletives here
@Sam the parentheses. You don't need an enclosing pair, you can just have if expression: thing
In C and friends the braces around the if body are optional, so you need to parenthesize the condition. That's not necessary in python since there's always a colon separating the condition from the body.
 
10:22 PM
@Arne I don't disagree! I meant I could see uses to increase clarity/readability, not so much uses where it would otherwise be unclear.
 
10:50 PM
ah, I see. I guess I was a little terse, it's of course a good idea to increase readability, no argument there
 

« first day (3494 days earlier)      last day (1459 days later) »