@Null yes...if you do a few things. First, you have to change the permissions on the file so it is executable using the chmod command. And you need a "shebang" line at the beginning of the file to tell Linux what python interpreter to use.
Hey there. I don't think it's worthy of a question but does anyone have some good intermediate resources on multiprocessing in Python?
Essentially if I hit a problem in a child process created with Pool() I want it to exit the parent process immediately. Currently it only gets raised when all the other processes finish
Bc at the moment I can be waiting hours only to discover that half the processes failed...
What is the process for integrating sentiment analysis in a CRM? What I am searching for is a system which analyzes the customer comments or reviews using the CRM and finds out the customer sentiment on the services provided by the system or company or a product.
@Arne I worked for a major retailer and they ploughed months into this kinda thing and were still refining it. Is it really a case of a couple of days?
It was also a product recommendation system so I guess that added complexity
@shuttle87 the worst was doing it for a startup in Dubai. No address system and customers would scream at drivers to go 20+km from where they actually asked for something to be delivered. You start to wonder why you bothered with the fancy algorithms :P
it's interesting I get contacted from time to time about this type of thing and I notice that a lot of solutions implicitly encode various regional factors into their systems without realizing it
So for example I worked on something in a remote area in Australia, the european system assumed that people could always get back to the depot within 1 day. This makes sense when you can drive across a country in 2 hours, we had things travelling 15 hours in one direction
Or your example of needing fuzziness about the delivery locations and times
My system was beyond fuzziness. The company needed to just empower drivers to say "screw you" but they had a "wall of shame" if they didn't deliver a certain percentage of their parcels
The company either needed to decide it wanted efficiency or was going to bend over backwards for customer service. In the end I had to bail
Dubai is an interesting one though because their road system means that 10m accuracy in GPS could put you on either side of a dual carriageway and there are no turning points, so 10m could end up being 10km out :P
Combo of both, often make some sort of solver backend that uses CPlex or Gurobi or something open source like minizinc and then do a bunch of piping data in and out of it
Been a couple of years since I was in the middle of all that but I likely have a job coming up soon with my company that is in that space
If I have a function that takes a file path or a file object as input, what's the nicest way to write that function correctly? (i.e. use a context manager if the input is a file path, but no context manager if the file is already a file object)
def read_file_data(file):
def do_stuff(file):
return file.read()
if isinstance(file, str):
with open(file) as file:
return do_stuff(file)
else:
return do_stuff(file)
^ Current solution
inb4 "who cares, just use a context manager in both cases"
That "which you don't know it exists" reminds me of the SQL equivalent for a FORALL quantifier: NOT EXISTS x ( NOT p(x) ), which is a pain to reason about every time.
And that reminds me of writing SQL queries with 5 levels of nesting for Uni homework *shudder*
(Though to be fair there was probably a smarter way to write those queries)
Apparently my more secure clone of the pickle module doesn't like my fancy OOP units:
class Config:
def __init__(self, max_upload_speed: units.megabytes/units.seconds):
self.max_upload_speed = max_upload_speed
load(dump(Config(0)))
# DeserializationRefused: Attribute "max_upload_speed" should be megabytes_per_second, but is int
Looks like I know what I'll be doing the next 4-5 days! Hooray!
@AkashPatel If you are looking for something at work, and there is a budget for it, I would suggest proposing spending money on the IntelliJ license. You get the full suite of features. Even if you think you might not need it because you are mainly working in Python, you'll be surprised how many other features you think you should have out-of-the-box that you don't get with a purchased PyCharm license
I would at least do a proper comparison between IntelliJ and PyCharm to ensure you make the proper choice between the two if this is a work-based purchase.
answer = "https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python/48870616#48870616"
question("What's wrong with this answer ? I love it: ", answer)
Morning cbg to all. I was once told "If you're the smartest person in the room, then you're probably in the wrong room." It dawned on me yesterday that during the time I'm in this chat room I'll never have that problem, unless I'm the only one here :)
Hey, I've got a server running a python background process, now "quite" often after 3-4 days of running the process "halts", as in it still runs but it doesn't do a thing anymore. If I attach strace to redirect the output to a new shell the output is empty. This runs inside an (alpine linux) docker, can I attach a debugger to the process? - Or even kill the process and just see what line the python function crashed on?
If I restart teh python process it works happily so I can't just do that.
@Arne uuuh the script is already running, starting a new one doesn't give the problem, and it must be an infinite loop somewhere, the script never "exits".
Nor does it throw an exception (at which case the server would catch the exception and handle it)
Well... no, it's a background service in the server that is supposed to keep track of all changes to an external database (the external database doesn't fire an event when a change occurs though, so it just keeps checking all data), and on a change some event is fired.
The "outer loop" should log a "keep alive + status update" into a log file, I notice the log file is ~10GB is size and then it just stopped no more logs and I also notice it doesn't reflect the updates anymore.
The reason is actually really simple: If you change the signature, it's not a functioning metaclass anymore. You simply cannot use it as a metaclass, because that'll just throw an error.
class Class(metaclass=MyMeta):
pass
# Traceback (most recent call last):
# File "untitled.py", line 15, in <module>
# class Class(metaclass=MyMeta):
# TypeError: __new__() takes 1 positional argument but 4 were given
So in the end it's really just a stupid way to write a class factory
ok, but it prints now bunch of : <selenium.webdriver.remote.webelement.WebElement (session="d0bedc6b7fae289264053f7be0808e16", element="0.9652430426341598-1")>
@Andie31 Sorry, but I can't solve every little problem for you. If you want to work with the web, you need to learn how it works. An element's text is completely different from the element's attributes. You have to understand what you're doing if you want to be productive.
TIL the guy from super considered harmful likes to call unknown constructors with arbitrary arguments
> That misconception causes people to make two common mistakes. > > 1. People omit calls to super(...).__init__ if the only superclass is 'object', as, after all, object.__init__ doesn't do anything! However, this is very incorrect. Doing so will cause other classes' __init__ methods to not be called.
If you blindly call super().__init__(...) in your constructors, then nobody can ever use your class with multiple inheritance, because you end up passing wrong arguments to the other parent class's constructor
So you only call super().__init__ if your base class is notobject.
uh no, since it means that if class A has parents B & C which are both derived from a common ancestor (object), then only C (Or B)'s constructor will be called (from A's super() call), the other branch won't be called.
So you'll have to call against object or make another "common ancestor" that all classes will derive from.
you don't know where you will appear in the MRO, and you have no way to know this ahead of time (unless you have a time machine that can tell you about all the code that will be written in the future which inherits you)
call unknown constructors with arbitrary arguments is a part of cooperative inheritance, it's a necessary evil.
btw I'm all for not ever using positional arguments in constructors. Especially once you start using inheritance it becomes impossible to use them. (As you noted)
Calling `super().__init__` in a class that inherits from object does two things: 1) It enables dependency injection between that class and `object` 2) It prevents multiple inheritance with two base classes that aren't designed to work together
@Aran-Fey as paul23 mentioned, dependency injection is one example
OrderedCounter, discussed in this room recently, is an example of dependency injection which only works because of super - an OrderedDict (which is a subclass of dict) gets "injected" as the storage backend for a Counterin place of the regular dict which Counter would otherwise inherit.
it's quite a easy to follow, yet explains the advantage of cooperative multiple inheritance very well (better than I could), and on top of that it's easy to listen to.
@Aran-Fey You can make part of the API of your class that it can't be inherited from, or only single inheritance. That's not strange in the programming world at all.
But then not calling super().__init__() isn't incorrect either. That just means my class doesn't support cooperative inheritance. The interface of my class is that you have to call each constructor individually.
Similarly that we have the idea that members starting with _ in the name are supposed to be private and shouldn't be changed when code uses this class. That's also just part of the api
@Aran-Fey Calling each constructor individually leads to problems by itself, but not calling super().__init__() is indeed a way of saying 'don't inherit from this'.
@wim to be frank, using "keyword arguments instead of positional arguments" isn't only a "YAGNI" decision, since having applications where half the constructors use keyword arguments and the other half uses positional (or even a mix) I consider really confusing.
So changing to keyword arguments later isn't trivial and one might very well do it from the start (I consider keyword arguments better anyways).
It's a double edged sword. You end up with a very flexible ecosystem, most anything is possible, anything can be hacked until it's working or usable. And you end up with some very messy tooling because people just implement their features, patching upstream if necessary
> When you use it on methods whose acceptable arguments can be altered on a subclass via addition of more optional arguments, always accept *args, **kw, and call super like super(MyClass, self).currentmethod(alltheargsideclared, *args, **kwargs). If you don't do this, forbid addition of optional arguments in subclasses.
^ wot
If you always forward all of the arguments, you'll get object.__init__ takes no arguments at the end, won't you?
IE: I have A, and B derives from A. So B calls super.__init__(**kwargs), now a user of this api also derives from A, making class C. And he makes class D that derives from both B and C. now B's super call (when originating from D) will go to C -- thus the same problem occurs, where you cannot be sure what super() is actually calling when designing B
Calling parents directly is considered bad design though, since it opens a large can of worms when deriving from a class that does this.
The difference is that the author of Bchose to inherit from A. With that design decision in place, it's true that further child classes of B have to follow some rules. But inheriting from object is no choice, it's mandatory.
And supporting inheritance (by a user of your library) without supporting multiple inheritance is not the way I would suggest to go, as it violates least astonishment rule in python.
And since you can't know for sure how the inheritance tree works you shouldn't inherit from it at all (otherwise inheriting from the simple inheritance might cause problems later on).
The the problem shows in class Foobar(Foo, Baz) - but point still stands, you need to be constantly very very careful anywhere in your inheritance tree if you don't cooperate in a class.
Ok, so I would say it's a good thing that Baz's constructor won't be called. Foo and Baz are two completely independent classes that have nothing to do with each other. Calling each of their constructors individually is much cleaner and safer
But also in the case you get a diamond shape inheritance anywhere in your tree you'll have to use super() to prevent the "base" of the diamond to be called twice. Now consider I inherit from foo, as well as A, where A inherits from B & C and B & C both inherit from foo again.
Now to prevent foo.init to be called multiple times you'll have to use the MRO, which means super() which means that by not calling super() in your foo class your inheritance tree stops at that point.
> So lets add some other animals. I like Penguins. So lets add a Penguin. Hmm, where do we put it? A Penguin is Bird right? But well, a Penguin can't fly even the fact that he has wings. So we put it under Fish right? Because a Penguin can swim. But wait, a penguin can also walk. So actually we need a new subclass once again! Oh damn...
> Okay, now we have to implement Flying Fishes. Oh damn, that think can swim and fly? Crocodiles that also swim and walk? And now we also want to implement the "American Dipper". What is an American Dipper? Oh seriously? A bird that "flys" under water or be…
@paul23 It's true that some methods can end up being called multiple times, but how often is that really a problem? I would, once again, actually consider this a good thing. Imagine you have two classes A and B who both happen to inherit from C. Now if you create a subclass that inherits from both A and B, you may end up with an undesired diamond. If neither A nor B use super, both of them will work correctly. But if they do use super, A might end up calling a B method incorrectly
there are 2 forms of bad subclassing of others code: either the class author has intended the class to be subclassed and that's a bad api design. or the author has not intended the class to be subclassed and you're subclassing it anyway.
it's very "FFS!". Because in reality, I'm creating many queues (2 in the above example), and am altering just a couple of them to restrict the maxlen. Looks like I'll have to just recreate them
Much thanks @AnttiHaapala
rhubarb folks! I go back to the dank existence that is grad school