|
Munkeymon posted:There are roughly three possibilities here: In my experience it is almost always the last one, and it just happens because they have authentication on their backend--nothing especially clever or paranoid, any number of frameworks/packages would give that behavior by default out-of-the-box. All you have to do is mimic the login API calls just as you're mimicking the actual data fetching API calls. Usually opening a requests.Session and posting credentials to the login URL will put the authentication token in your session automatically. You typically don't even have to find it--just post your credentials (inside a session so you have persistent headers) and from then on you're authenticated. Sometimes for the authentication you will find a situation like your second one, where you have to fetch the login page, find some token, then include it when you post your credentials. Edit: actually I've been assuming that this is a website that requires a login. If it doesn't then I agree, you're probably in the second scenario there. SurgicalOntologist fucked around with this message at 20:22 on Jan 11, 2018 |
# ? Jan 11, 2018 20:20 |
|
|
# ? May 16, 2024 18:16 |
|
Is there a canonical way to parallellize a for loop that is compatible with 2.7? I've seen stuff like multiprocessing, concurrent.futures, and all sorts of stuff, but no idea which is the preferred option. The loop has another loop inside it, but it's over like ten elements so it's not worth optimizing that part.
|
# ? Jan 12, 2018 19:51 |
|
ufarn posted:Is there a canonical way to parallellize a for loop that is compatible with 2.7? I've seen stuff like multiprocessing, concurrent.futures, and all sorts of stuff, but no idea which is the preferred option. No, because it's dependent upon what kind of stuff is happening in the loop.
|
# ? Jan 12, 2018 20:12 |
|
I have never had a need to do this, but I thought it was neat: Recover python source code if you've deleted the file but it's still in memory.
|
# ? Jan 12, 2018 20:38 |
|
If I have a container class, holding some instances of another class, is there a simple way to get the container to aggregate the results of the inner classes, without writing separate methods for the container for each inner class methods Class Inner: Def spades(self): Return 'spades' Def hearts(self): Return 'hearts' Def diamonds(self): Return 'diamonds' Class container: Def __init__: Self.children = [Inner(), Inner(), Inner()] Def spades(self): Return [inner.spades() for inner in self.children] .... How can I avoid writing similar methods for hearts and diamonds, on the basis that the logic will be the same for all three? Preferably in a way that still works with code completion in Pycharm?
|
# ? Jan 13, 2018 00:31 |
|
I'm so dependent upon formatting and syntax highlighting that I literally cannot read that post without significant effort.
|
# ? Jan 13, 2018 01:26 |
|
Thermopyle posted:I'm so dependent upon formatting and syntax highlighting that I literally cannot read that post without significant effort. I mean without indentation it isn't valid Python anyway
|
# ? Jan 13, 2018 01:41 |
|
|
# ? Jan 13, 2018 05:46 |
|
Do you know about getattr? Python code:
accipter fucked around with this message at 06:44 on Jan 13, 2018 |
# ? Jan 13, 2018 06:37 |
|
Yes, I'm familiar with it, but I was hoping to have something a bit more introspective on the Inner class that gave me code completion on the container class. Maybe I'm asking for too much.
|
# ? Jan 13, 2018 06:57 |
|
ufarn posted:Is there a canonical way to parallellize a for loop that is compatible with 2.7? I've seen stuff like multiprocessing, concurrent.futures, and all sorts of stuff, but no idea which is the preferred option. There are lots of options but I've always found multiprocessing the easiest to use and most widely applicable. If you have no idea what to use and don't want to explain the problem further then maybe give that a shot
|
# ? Jan 13, 2018 07:48 |
|
QuarkJets posted:There are lots of options but I've always found multiprocessing the easiest to use and most widely applicable. If you have no idea what to use and don't want to explain the problem further then maybe give that a shot
|
# ? Jan 13, 2018 10:54 |
|
Edit: Turns out the issue wasn't what I thought it was.
Wallet fucked around with this message at 17:09 on Jan 13, 2018 |
# ? Jan 13, 2018 14:06 |
|
Slimchandi posted:If I have a container class, holding some instances of another class, is there a simple way to get the container to aggregate the results of the inner classes, without writing separate methods for the container for each inner class methods What are you trying to do here? Your example makes little sense to me. I'd probably have each suit (spade, heart, etc) be a class that inherits Inner, setting an internal class variable common to Inner to also denote suit and value.
|
# ? Jan 13, 2018 14:10 |
|
Or use ('Hearts', 12) for the Queen of Hearts Then if you use a set() instead of a custom container, you can write {card for card in deck if card[0] == 'Hearts'} If you want, use a namedtuple so you can use card.suit or card.rank
|
# ? Jan 13, 2018 16:18 |
|
I've made a simple text game. It works just fine in a single file. I've basically copied the whole class called Engine to a separate file (called Engine.py) and tried to import it as you can see. Ex45.py code:
code:
code:
|
# ? Jan 15, 2018 11:31 |
|
Assuming you're importing Ex45 into Engine.py too (since you're calling Intro() and that class is defined in the other file), you've got a circular dependency - both modules depend on each other There's an explanation here with examples - the problem is you start to load file A, and it gets to the import statement and has to load file B. File B wants to import A - and that's fine, it's happened already, but it hasn't finished loading A yet. So you get to the line where it tries to use something from file A, Python hasn't seen that function or class yet (it doesn't exist as far as it's concerned), so it throws an error At this point you're probably thinking ugh this is bad and complicated and why did I bother - and yeah, often it's better to keep things in the same file. Or you have to organise the project so you don't get circular dependencies. If it's unavoidable, a lot of people recommend putting your imports at the end of the file (so they only run after the file has been fully loaded)
|
# ? Jan 15, 2018 12:32 |
|
baka kaba posted:Assuming you're importing Ex45 into Engine.py too (since you're calling Intro() and that class is defined in the other file), you've got a circular dependency - both modules depend on each other Thank you. That makes perfect sense. I'd tried importing and got circular issues so tried briefly to see if I could fix it. Not worth the effort but something to keep in mind later. I did manage to export a different part to a separate file so that's all good. Sad Panda fucked around with this message at 14:24 on Jan 15, 2018 |
# ? Jan 15, 2018 14:20 |
|
I don't know the full extent of your structure but you can also do imports inside of functions that need them to avoid circular imports.
|
# ? Jan 15, 2018 14:50 |
|
This is annoying. Why does this works fine:code:
Python code:
code:
unpacked robinhood fucked around with this message at 18:18 on Jan 17, 2018 |
# ? Jan 17, 2018 18:03 |
|
unpacked robinhood posted:This is annoying. Why does this works fine: I barely looked at your code, but is there some reason you're not using python-requests? It's so widely used I'm surprised it's not in the standard library yet. Thermopyle fucked around with this message at 18:22 on Jan 17, 2018 |
# ? Jan 17, 2018 18:06 |
|
Thermopyle posted:I barely looked at your code, but is there some reason you're not using python-requests? It's so widely used I'm surprised it's not in the standard library yet. I..didn't know this was a thing. I used urllib3 for the rest of my project and it worked good enough.
|
# ? Jan 17, 2018 18:08 |
|
unpacked robinhood posted:I..didn't know this was a thing. I used urllib3 for the rest of my project and it worked good enough. It's been so long since I used urllib3 so don't take this to mean much, but I don't see anything wrong with your usage here. They could be doing something stupid like white-listing user agent headers, but it'd be weird that they'd whitelist curl but not whatever UA urllib3 sends. FWIW, Chrome loads that url fine here.
|
# ? Jan 17, 2018 18:12 |
|
Thermopyle posted:It's been so long since I used urllib3 so don't take this to mean much, but I don't see anything wrong with your usage here. I know, it loads in any browser vOv. Seems to work perfectly with requests, thanks ! Do you mind scramling the API url a bit in your previous post (Or quote again my edited post) ? This is another of those unprotected services they explicitely sell and I'd rather not have the URL show up in google results too much. e: thanks for the edit unpacked robinhood fucked around with this message at 18:24 on Jan 17, 2018 |
# ? Jan 17, 2018 18:17 |
|
Scrambled.
|
# ? Jan 17, 2018 18:22 |
|
as pointed out to me in the general question thread, we have a python thread, so here goes. I have formatting configured in a json file to specifically add the logging.Formatter as: code:
code:
|
# ? Jan 17, 2018 19:28 |
Roundboy posted:as pointed out to me in the general question thread, we have a python thread, so here goes. The problem here is that you're specifying behavior for logging.Formatter that relies on information that logging.Formatter doesn't have. Instead, you should probably specify formatting behavior for a subclass of logging.Formatter that you control. Logs you create will use the special behavior, but other logs will be unaffected.
|
|
# ? Jan 17, 2018 19:32 |
|
Trying to read up on abstract base classes, found a few tutorials and Pycon videos but they all seem a bit shallow in their explanation. Have I got this roughly right? - Abstract base classes are designed not to be called directly, but inherited from. ABCs force derived classes to implement their 'abstract' methods. - If any of these method is not implemented in a derived class, TypeError is thrown when the program tries to run. - An ABC may fully implement some of these abstract methods, or simply pass, leaving the derived class to handle the implementation. - Likewise, the derived class may override an abstract method, or make a call to super() and use the ABCs implementation directly (inheritance of abstract methods is not permitted) If I've got this correct (and that's a big if), then I can see the use of ABCs when are writing classes that others will use or have complex structures, as you need to enforce those methods for the whole thing to work. But even if the object I'm describing in my class is 'abstract' (e.g. Bird), which I never call directly, and I only use it to subclass instances of 'real' birds (Gull, Eagle, Owl), this situation wouldn't benefit from using an ABC. I would be better off with a standard Bird class that I inherit from, right?
|
# ? Jan 17, 2018 20:46 |
|
unpacked robinhood posted:This is annoying. Why does this works fine: You goofed a bit when editing it and have part of the path in the HTTPSConnectionPool line, which causes a HostChangedError exception. After correcting that and tweaking the URL a bit to point at a valid destination on a server I control, I see definite differences in the request and the resulting server responses in my nginx logs. code:
I then cut down the base_host line to remove the host and leave only the path and I get this: code:
Here's another test from a third-party service: code:
I've switched it to HTTP so I can see the actual request data easily and here's what's showing up in Wireshark... Here's the request your code is sending: code:
code:
code:
I did that by adding code:
So in summary it looks like your requests were set up wrong so it was requesting the URL as the path which is the cause of your 404, and possibly the user agent thing as well. wolrah fucked around with this message at 20:53 on Jan 17, 2018 |
# ? Jan 17, 2018 20:51 |
|
Slimchandi posted:But even if the object I'm describing in my class is 'abstract' (e.g. Bird), which I never call directly, and I only use it to subclass instances of 'real' birds (Gull, Eagle, Owl), this situation wouldn't benefit from using an ABC. I would be better off with a standard Bird class that I inherit from, right? I wouldn't use an ABC unless at least one of the methods was going to be abstract. In this case the advantage of an abstract foo over def foo(): pass in the base class is that python will yell at you if you forget to implement foo in the subclass, instead of just calling the empty method and plowing ahead. If this is not a thing you care about then you have no reason to use ABCs.
|
# ? Jan 17, 2018 20:57 |
Slimchandi posted:Trying to read up on abstract base classes, found a few tutorials and Pycon videos but they all seem a bit shallow in their explanation. You've got it. Slimchandi posted:
Alex Martelli, in Luciano Ramalho's Fluent Python, pg. 331 posted:"ABC's are meant to encapsulate very general concepts, abstractions, introduced by a framework - things like "a sequence" and "an exact number". [Readers] most likely don't need to write any new ABCs, just use existing ones correctly, to get 99.9% of the benefits without serious risk of misdesign. Eela6 fucked around with this message at 21:05 on Jan 17, 2018 |
|
# ? Jan 17, 2018 20:58 |
|
Eela6 posted:The problem here is that you're specifying behavior for logging.Formatter that relies on information that logging.Formatter doesn't have. makes sense, but is this possible to hold this in a config.json or will that formatter need to be imported / added to each script ? Originally it was nice to have a central file controlling all the logging formats, but I inherited this and trying to fix it in a unified way.
|
# ? Jan 17, 2018 21:33 |
|
Thanks. Fluent Python is a mystery to me. I look at the next chapter, read a few pages and think 'where am I gonna use that sort of crazy thing in my code?'. A few weeks later someone posts a section of it explaining the very thing I'm stuck on. Probably happened 3 or 4 times now.
|
# ? Jan 17, 2018 21:56 |
It is an extremely through text. There's a lot to take in.
Eela6 fucked around with this message at 22:47 on Jan 17, 2018 |
|
# ? Jan 17, 2018 22:45 |
|
Thanks for the various commented tests. It feels like I double checked those lines a dozen times but missed this. I'm using requests now, which works so far.
|
# ? Jan 18, 2018 21:59 |
|
unpacked robinhood posted:I'm using requests now, which works so far. Very good move. To whoever was asking why it wasn't in stdlib earlier: https://speakerdeck.com/kennethreitz/python-requests-and-the-standard-library
|
# ? Jan 18, 2018 22:14 |
|
The March Hare posted:Very good move. Thanks for that. I figured it would boil down to "putting in the stdlib involves losing agility". I get it.
|
# ? Jan 20, 2018 16:58 |
|
One could use that argument to exclude anything from the standard library.
|
# ? Jan 20, 2018 19:32 |
|
Does anyone know if pipenv can replicate the behaviour of vex? (https://pypi.python.org/pypi/vex) I've always found vex to be lightyears ahead of virtualenvwrapper for development (activating and deactivating virtualenvs manually is a horrible pattern) but I've never used pipenv and people seem very happy with it To be clear, the behaviour I want to replicate is Bash code:
Bash code:
NtotheTC fucked around with this message at 15:22 on Jan 21, 2018 |
# ? Jan 21, 2018 15:18 |
|
|
# ? May 16, 2024 18:16 |
|
NtotheTC posted:To be clear, the behaviour I want to replicate is I have endless copies of code:
|
# ? Jan 21, 2018 17:08 |