@ParitoshSingh Sorry for delayed reply (i was sick past few days). However I tried to execute it but i came across trouble for step three where I have to find % difference between difference values and the threshold. This is what i did.
@ParitoshSingh #Initialize date values and find difference between two columns date_value = df.iloc[:,4:].values np.abs(np.diff(df[date_value].values))
#Find % difference between difference between values and the threshold. target = new_df4[['Target']].values np.abs(np.diff(df.iloc[:,4:].values)) > target
See i believe that finding % difference between difference values is wrong here, because i am finding values that are greater than target. I request you, Could you please assist me on what i should be doing?.
Quick context: From the issue discussion/comments/test-code it seems like InitVars and ClassVars are somehow similar, and I just don't get why. Of course a ClassVar doesn't need a factory, since there is only one. But there is one InitVar per instance, so why would it make no sense to have a factory there? You can call it in __init__, and I don't see how it could go wrong.
I'm just learning Python. But I made a small game I uploaded it to pygame, but I'm still working on it. https://www.pygame.org/project/4110 What do you think about it?
Is assigning a result of a list operation to it's own slice an inplace operation? For example if I want to reverse only the latter half of a list, will this be considered inplace?
In [72]: lst = [1,2,3,4,5,6]
In [73]: lst[3:] = lst[3:][::-1]
In [74]: lst
Out[74]: [1, 2, 3, 6, 5, 4]
since I am not creating an extra list, and updating the older list itself
I am preparing for interviews, and lot of questions ask for in-place, or O(1) space solution, so I was wondering if this approach will be considered as such
and I literally said "if in-place == O(1) space ..." which suggests that this is one possible interpretation
off the top of my head I'd have called it in-place because you end up with a mutated original list, but Aran's remark and your O(1) thing suggests that this is probably wrong
@DeveshKumarSingh try with the easier one, and if it fails due to memory implement the harder one. They are probably sloppy and just want you to mutate
def reverseString(s):
"""
Do not return anything, modify s in-place instead.
"""
i = 0
j = len(s) - 1
while i <= j:
s[j], s[i] = s[i], s[j]
i += 1
j -= 1
@AndrasDeak Yeah, but I'm thinking the slice assignment is probably converting it to a list internally because it (probably) wants to know how many elements you're assigning
So am I correct in assuming that using list slicing by assigning back to a slice is using extra space, albeit temporarily so it's not O(1) space in true sense in terms of the whole function?
or even this is upto the interpretation of the question and the interviewer?
there's only one sense of O(1) space, and it's not
The interpretation only applies to vague notions such as "in-place". O(1) time or space complexity is exact.
Think of it like this: O(1) time means that if your list occupies 90% of your memory you'll probably be able to do the operation anyway. O(n) means you can't.
got it, where O(1) space is in reference to the size of the input, making copies by using say list slicing anywhere in the function makes in O(n) space complexity
now what I just said is not exact, just a hint of how you might think about the difference
@DeveshKumarSingh if you create a list with size m then you get a memory hit of m. If m scales with n then you are in trouble. If m doesn't scale with n but you create the m-sized list k times, where k scales with n, you again have O(n).
To be exact, you have to count the total memory need (in whatever units) of your algorithm, and look at the end result and see how it behaves for large n as a function of n, where n is the characteristic size of your input.
interesting, i wasn't aware that we could pass a step size on the lhs, never tried it before. slicing lists on rhs will create a copy, so that's occupying some space. that notation on the lhs, i'd imagine is not using any extra memory. i don't know for sure yet though
if (a == b) {
/* Special case "a[i:j] = a" -- copy b first */
v = list_slice(b, 0, Py_SIZE(b));
if (v == NULL)
return result;
result = list_ass_slice(a, ilow, ihigh, v);
Py_DECREF(v);
return result;
}
Otherwise you'd run into trouble while mutating the list item by item. In any other case you don't have to do this, you can even put generators on the right-hand side and they'd be consumed item by item I think
The left- and right-hand operands. In general, a[i:j] = b
/* a[ilow:ihigh] = v if v != NULL.
* del a[ilow:ihigh] if v == NULL.
*
* Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
* guaranteed the call cannot fail.
*/
@AndrasDeak yes makes sense, hence the sizes don't match up for the slice assignment which I suppose means that the size of LHS is calculated before the slice assignment
As frustrating as the tag can become, you'll gain nothing from posting that link. It's better just to leave the question feed if you're getting frustrated
Thanks, makes sense. I was just frustrated couple minutes ago, because I literally saw 6 questions in a row which were tophit at google. Now I have common sense again, I will probably never post that link. @roganjosh
about 2 weeks ago, you asked for volunteers to review the job postings on the psf jobs board; I stepped forward, and you replied that you had a few things to tie up first, and would come back to us.
For those who saw my questions about pulling data from a Flask server that was providing data using stream_with_context, I came up with the approach for the client side using requests.
You can use a vanilla requests.get() call, and you will pretty much skip all the streaming aspects - requests will get() repeatedly until all the data has been streamed, and then the client proceeds as normal.
But if you want the client to stream also, then: 1. call requests.get with stream=True 2. repeatedly call resp.iter_lines or resp.iter_content to get the stream in chunks (where resp is the response returned from requests.get)
iter_content uses a default chunk_size of 1, so if you don't set this, performance will be, um, poor. iter_lines uses a default chunk_size of 512, but delivers data line by line.
@roganjosh No, it's just that the server has to build one big response first, instead of yielding a bit at a time.
There is still just one call to GET
At least as far as the client code and server code are concerned - may be more going on under the covers. I may delve a little further today with packet capture and Wireshark to see.
It's just generally more constructive to have stuff return asap and have the client connected to a server side only websocket and then push to that instead
I certainly hope not! I keep the back door open because my house tends to get the sun on the bricks all day. I've tried draping a duvet cover over to keep things out but the poor thing found its way in anyway. Its mates were on the outside of the window trying to break it out; thankfully easy enough to catch and free
one flat I used to live in, because of where it was, every bloody time we had the windows cleaned, you could almost guarantee a bird would fly into it and break their neck :(
I guess it depends on how far through the room they can see. Perhaps the ones that I have seen, only intended to land on the windowsill on the inside of the window, not go significantly through the house
@Sosi it's part of the standard library so you should already have it
@piRSquared But now I've gone back to find the proof that's an important distinction that I should have made. That's my bad, sorry. I think I overstated the fact in my head
@JonClements I doubt you missed it, but that song was used at the end of Kodi Lee's performance on America's Got Talent, which is something of a tear-jerker
I would assume that will exceed the scope of our records. It's probably a best-guess, I'd probably either go for a Beatles song or something like Slade - Merry Christmas
In some cases where it is viable that a 404 may be returned (by design) there I use it but in other cases do I make it except requests.exceptions.RequestException which is where all exceptions inherit from
If you're going to wrap every request in your own try/except then you should consider a function that does this for you, instead of repeating the same construct over and over.
That is, if your handling of the exception is the same every time
def main():
#whole program goes here
try:
main()
except Exception as e:
print(e)
log_error(e)
If you don't print the stack trace I strongly recommend saving it somewhere because you're not doing anybody any favors by discarding diagnostic information
@roganjosh I'm not sure how to explain but it's one request if everything goes okay and a series of them if it doesn't. For example say you do a GET request against an english dictionary with a string. If it is a word you get a 200, if not, the program then queries a backend server as a fallback and if it is not found even then, it opens a github issue on the repository.
So the API expects single word searches. Why would you promote invalid input to a github issue?
You can filter out multi-word inputs to your server and just send back a signal that the input is not valid
That doesn't even have to be an exception; the API input doesn't pass your tests, so you tell the client that and just reject it. You don't crash your server
I think there are three categories of input here. Words such as "coconut", which return a 200 response; word-like input that isn't in the dictionary, such as "florby"; and invalid input such as "123 x \r %^#$%&\0".
And if you're the one that's sending the request, then this is also an easy-enough situation to handle because you can catch the invalid input prior to making the request in a huge majority of cases, and pass the error back to the user if it fails
Anyway, this is all incidental to the point I was making. Exception handling is not magic and it can not be used as a substitute for clear design requirements. If you don't know what kind of errors you want to catch, and you don't know what you want to do when you catch an error, then don't bother writing an exception handler.
@aadibajpai who's your target audience with the app? this sounds like it is technical people who can stomach seeing a traceback. If it is non-technical people, convert the error to a nice error message.
The dictionary thing was just a simplified example. When I say crash what I mean is that the client runs the script and it fails with the requests error which isn't handled.
@MisterMiyagi Nope, end users just need to have python installed.
So for example the whole ConnectionError message when there's an internet problem may be confusing to a person who's not experienced with that. In that case I'd want to output something like check your internet or so.
Whereas if there's an HTTPError the first time then the script would know it should query the backend since the appropriate response wasn't received
@Kevin This was what I was confused about mainly, like in the above cases I have an idea what I want to do. However I wasn't sure if every requests call should have a general try/except or no.
Traceback (most recent call last):
File "C:/Users/aadibajpai/.PyCharm2018.2/config/scratches/scratch.py", line 5, in <module>
bad_r.raise_for_status()
File "C:\Users\aadibajpai\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: NOT FOUND for url: httpbin.org/status/404