|
Is the smtplib module not thread-safe or something? I have a little monitor script set up to monitor some SQL servers and send out an e-mail if there's a long-running query that's taking up too much time on the server. Everything works fine, but when I added smtplib into the mix, everything went all wrong. As soon as the monitor thread sends an e-mail, the script just seems to stop execution shortly after the server.quit(). It sends that e-mail fine (with some formatting issues, but I'll fix that poo poo later) and I see all the debug output on the terminal from set_debuglevel(), and I see it print "E-mail successful" as well (because I added that after the server.quit() to see if it was hanging on waiting for the connection to time out or something), but it never exits the enclosing elif. It's not using any CPU time or anything... it just doesn't do anything. Here's the relevant code:code:
|
# ¿ Apr 9, 2008 16:27 |
|
|
# ¿ May 14, 2024 23:14 |
|
m0nk3yz posted:Chutwig Regarding smtplib - did you look at this thread on c.l.p? After I posted, I went back and googled some more and found that discussion. I'll probably just write another script external to this one to handle e-mailing and invoke it through subprocess, or just invoke sendmail directly, or something.
|
# ¿ Apr 9, 2008 21:09 |
|
fletcher posted:Hmmm I think it's because this csv file was actually latin1 encoded even though the dude said it was UTF-8. Maybe it starts with a UTF-8 BOM but is really ASCII? If you hexdump it and it starts with EFBBBF, file might see that and just stop there.
|
# ¿ Jun 30, 2015 03:47 |
|
superstepa posted:What I want is to have one main class that only defines a few very basic methods and variables and then have a large bulk of extra methods/variables appended to an instance of that class post initialization. I know you can just use setattr but I was wondering if there is an "official" way to do that. This sounds a little bit like you want to create an abstract base class and then create subclasses from that. What's the underlying problem you're trying to solve?
|
# ¿ Jul 19, 2015 23:41 |
|
Fergus Mac Roich posted:So, Python mavens, is this the most elegant way to do this? It seems like you wrote countit() the way you did so that you could use tee() to not read the file twice, but with the consequence that now everything else in the function is doubled up. If you expect to re-use the file contents over and over, read it into a buffer outside the function and pass that in. There are also some things being accessed inside countit() that look like globals, e.g., fieldnames. Do the counts need to be done together like that? Is there a reason why they can't be done one at a time? If the state of one counter has no bearing on the other, then I would advise doing them separately instead of doubling up. Generally speaking you will want to deal with having 0 things, 1 thing, or more than 1 thing. Right now you're dealing with 2 things as 2 things, instead of dealing with them as 1 thing twice. Changing countit() to handle one counter as a time is a more generalizable solution.
|
# ¿ Sep 4, 2015 00:36 |
|
Fergus Mac Roich posted:What do you think of this? I think that is a lot more reasonable, yeah. Obviously there's such a thing as quick hacky scripts, but I've been burned by wackity global variable scope too much in the past and prefer to pass arguments around explicitly.
|
# ¿ Sep 4, 2015 00:54 |
|
Emacs Headroom posted:Flask might feel a little underpowered. Flask intentionally deals with a much smaller subset of the problem domain than Django does. As you said, Django is a large framework, and it provides a lot of different things that Flask by design does not. You can do anything in Flask that you can do in Django, but you'll need to do a lot more of your own plumbing. Some people prefer it that way. I've published Real Applications™ in Django and tinkered with Flask, and I like the lightweight feeling of Flask. Django's out-of-the-box admin site is still one of its huge selling points, though, and Flask won't ever provide that. Also, to your point about Angular/React/Ember stuff, there's no reason why you can't have the frontend of your site written using one of those while the backend is Django/Flask/Rails/something else that emits JSON to the frontend framework. It's not really about the site itself being written "in Python", since that's all on the backend and you're not going to be able to do anything dynamic without JS.
|
# ¿ Sep 7, 2015 02:00 |
|
Thermopyle posted:PEP-484 makes tooling (like PyCharm) much better and is now in 3.5 I've been using it on Python 3.4 with PyCharm for a year or so and it's only gotten better. I really hope that at some point the interpreter will have a mode to enforce the type signatures if present, like a -t flag or something to that effect. Cingulate posted:Is there (gonna be) a from __future__ import type_annotations thing? Also why do people continue to use python 2 For Python 2? It's not getting anything new at this point and adding annotations to 2 would require a lot of work, the type signature stuff builds on the generic annotations that 3 introduced. As for why 2.x is still used... though most OpenStack stuff has to pass gates at both 2.7 and 3.4 now so it may be usable with 3.x by Mitaka, Twisted seems to be coming along (and a lot of what it does is subsumed into 3.5 anyway with all the async/await functionality), and Mercurial will probably eventually be clubbed into submission by the core Python developers.
|
# ¿ Sep 14, 2015 02:29 |
|
Thermopyle posted:You can use MyPy to check types for you. I know MyPy exists, but it'd be even nicer to have something blessed in CPython that says This Is An Okay Thing. Python's obviously survived a quarter century (jfc) without it, but it'd be a nice-to-have thing.
|
# ¿ Sep 15, 2015 01:06 |
|
Are you working in Python 2 or 3? You're manually inheriting from object which implies Python 2, but calling super() without arguments which implies (really requires) Python 3. My best guess is there's some weird inheritance diamond stuff going on. What happens if you change the super call to super(Inventory, self).__init__()?
|
# ¿ Sep 20, 2015 21:13 |
|
hooah posted:When I run this, I get a TypeError on the last line: "randBinList() missing 1 required positional argument: 'n'". I get that randBinList isn't getting its argument, but I don't understand why. I thought the initRepeat should take care of that? Python code:
Python code:
|
# ¿ Sep 23, 2015 02:26 |
|
Popular Thug Drink posted:i'm trying to make a palindrome tester, what am i doing wrong here? Here is what I would consider a more Pythonic palindrome tester (not vigorously tested but seems to work); that is, it uses a comprehension (use comprehensions everywhere). This is Python 3 code, but will work in Python 2 if you remove the annotations from the method signature and use def is_palindrome(palindrome): instead: Python code:
The way this works is as follows:
It is surprisingly common to be able to distill logic into a comprehension in Python, and it's a really valuable pattern to use; smart use of comprehensions is one of the best things you can do for keeping things concise in Python, since the rest of the language can require some extra verbosity that other languages don't. Importantly, it also forces your logic into a single line that reads pretty naturally with a little experience: you can read the comprehension as "this will return a list of boolean values that say whether the character at positions i and the mirror of i were the same while comparing characters in the string through the first half of the string".
|
# ¿ Oct 14, 2015 04:03 |
|
Suspicious Dish posted:
the magnificent burn of being ultra code golfed
|
# ¿ Oct 14, 2015 23:31 |
|
QuarkJets posted:Numpy is Python Probably what he means is "can be done with the Python standard library without needing to install anything".
|
# ¿ Oct 15, 2015 23:48 |
|
What's the right way to express a complex type hint for a dictionary of the following format:code:
|
# ¿ Dec 7, 2015 00:44 |
|
Ah, I understand now. I re-rendered it as Tuple[str, Tuple[int,int], Tuple[int,int]], since the names weren't important.
|
# ¿ Dec 7, 2015 02:16 |
|
QuarkJets posted:But you're basically creating a bunch of these tuples and then magic-indexing into them to get coord1 and coord2, right? You should really, really consider using a class instead; it'll be so, so much cleaner and easier to understand It's a one-off script for advent of code, not something that anyone but me will ever look at. I know it would be cleaner to create an actual class and pass that around, but for this use case a tuple with magic indexes is fine.
|
# ¿ Dec 7, 2015 19:24 |
|
Hammerite posted:Why do you need to use type hinting then? Just practicing expressing things in it. Does there need to be a reason? It's different syntax from what I'm used to with Java/Go, so I figured I'd try to formulate the appropriate Python type hints.
|
# ¿ Dec 8, 2015 00:58 |
|
baka kaba posted:From looking around, this is possible but it's pretty hacky, which makes it seem like you're not really meant to do this kind of thing. What exactly are you doing? If you lay out the general plan then people might have some more, uh, pythonic suggestions It's not hacky, it's just taking advantage of first-class objects, and you'd treat it as any other callable.
|
# ¿ Oct 7, 2016 18:00 |
|
|
# ¿ May 14, 2024 23:14 |
|
huhu posted:
You could think about using oslo.config, which is a configuration parser library used by OpenStack that is reasonably nice to use (I've used it in some projects). The downside is that it will drag in a number of other dependencies - not too many by OpenStack standards, but maybe more than you want to deal with: http://git.openstack.org/cgit/openstack/oslo.config/tree/requirements.txt
|
# ¿ Feb 26, 2017 16:58 |