|
Edison was a dick posted:Break only takes you out of the current for loop, you presumably have another loop around that, which you're not breaking out of. There's a really weird trick to break out of an outer loop without using a flag variable: using a for-loop else. With a for-loop else, the "else" happens just once after a for-loop is finished. But if you break out of that for-loop, the "else" is skipped. Which leads to this... code:
|
# ? Sep 20, 2016 00:34 |
|
|
# ? May 8, 2024 05:27 |
Is it straightforward to implement something so that when you have script.py that imports stuff from library.py, you can also get it to make you a copy of both library.py and script.py in the output folder? I'm specifically after doing it through Python, doing that via OS would be trivial.
|
|
# ? Sep 20, 2016 10:54 |
|
cinci zoo sniper posted:Is it straightforward to implement something so that when you have script.py that imports stuff from library.py, you can also get it to make you a copy of both library.py and script.py in the output folder? I'm specifically after doing it through Python, doing that via OS would be trivial. Is this what you want? to_copy.py code:
code:
|
# ? Sep 20, 2016 15:31 |
huhu posted:Is this what you want?
|
|
# ? Sep 20, 2016 16:09 |
|
PyBay 2016 vieos are finally going up, including my Bokeh talk </shameless promotion> Raymond Hettinger also gave an, ah, interesting keynote.
|
# ? Sep 21, 2016 01:20 |
|
I'm probably going to figure this out within 5 minutes after posting, but I've been struggling with inserting data into sqlite 3 and I'm not quite sure why. code:
code:
|
# ? Sep 21, 2016 08:54 |
|
I've never used sqlite3 so I'm winging this a bit, but I don't think your problem is with sqlite3. Looping over a dict doesn't work like that. The for loop will loop over each key in the dict:Python code:
Python code:
|
# ? Sep 21, 2016 09:08 |
|
ArcticZombie posted:I've never used sqlite3 so I'm winging this a bit, but I don't think your problem is with sqlite3. Looping over a dict doesn't work like that. The for loop will loop over each key in the dict: The test dictionary only contains 1 entry, but the real resultset will have tens of entries. I just started out with a smaller data set to spot problems early on. Your example should get me up and running though, I'm try and write something that inserts just the test dictionary and then see how I can write a loop around it so I can repeat it for multiple entries.
|
# ? Sep 21, 2016 09:47 |
|
The real results will be a list of dicts, no? You'll loop over the list, not over the dicts.
|
# ? Sep 21, 2016 09:53 |
|
ArcticZombie posted:The real results will be a list of dicts, no? You'll loop over the list, not over the dicts. yeah, you're right. It's a list containing dictionaries. I'm really new to this, and still struggling a bit with the different types of data structures (and how to proces them)
|
# ? Sep 21, 2016 11:34 |
|
LochNessMonster posted:yeah, you're right. It's a list containing dictionaries. since you're iterating through a list, just use arcticzombie's example for each dict in it: Python code:
|
# ? Sep 21, 2016 12:14 |
|
Man there are like 900 ways to install Py3 on OSX. I'm just going to brew install python3 and forget about it before I start to obsess about which is the best down the road. Never going to actually learn if I keep nitpicking my infrastructure :|
|
# ? Sep 21, 2016 12:58 |
|
Martytoof posted:Man there are like 900 ways to install Py3 on OSX. I'm just going to brew install python3 and forget about it before I start to obsess about which is the best down the road. Never going to actually learn if I keep nitpicking my infrastructure :| Anaconda will never let you down.
|
# ? Sep 21, 2016 12:59 |
|
I ended up settling on good ole dependable "brew install python3" which may or may not be terrible, I'll find out as I go along
|
# ? Sep 21, 2016 13:05 |
|
LochNessMonster posted:I'm probably going to figure this out within 5 minutes after posting, but I've been struggling with inserting data into sqlite 3 and I'm not quite sure why. It looks like other people squared you away on your for loop. I wanted to show you this option for inserting from a dict as well. Instead of the question mark "parameterization", you can use named placeholders. Those names are the keys of your dict. Python code:
Python code:
|
# ? Sep 21, 2016 18:35 |
|
Tigren posted:It looks like other people squared you away on your for loop. I wanted to show you this option for inserting from a dict as well. Instead of the question mark "parameterization", you can use named placeholders. Those names are the keys of your dict. Both options seem like a pretty efficient way of doing things. I didn't get the for loop working yet so I'll give this a try as well. Just for my understanding, the: in/out [number] is from your python environment?
|
# ? Sep 21, 2016 19:43 |
|
LochNessMonster posted:Both options seem like a pretty efficient way of doing things. I didn't get the for loop working yet so I'll give this a try as well. Ya, those numbers are from iPython, which is an alternative python REPL. You can ignore those parts.
|
# ? Sep 21, 2016 20:51 |
|
Tigren posted:Ya, those numbers are from iPython, which is an alternative python REPL. You can ignore those parts. That's the one thing I don't like about using iPython. It makes copy/pasting REPL sessions a little weird because of the in/out thing. Especially if you're copy/pasting to use in doctests.
|
# ? Sep 21, 2016 21:36 |
|
When using the for loops I keep getting the same error, no matter what I try. When running this: Python code:
code:
So I went and tried Tigrens suggestion on how to tackle this. It doesn't give any errors, but for some reason it doesn't really enter the values either. When I do a SELECT * FROM motorcyles; it only returns 1 bogus value I added manually. Python code:
LochNessMonster fucked around with this message at 10:19 on Sep 22, 2016 |
# ? Sep 22, 2016 09:54 |
|
Do you need to commit the transaction?
|
# ? Sep 22, 2016 10:36 |
|
Hammerite posted:Do you need to commit the transaction? I feel pretty stupid now...
|
# ? Sep 22, 2016 11:29 |
|
LochNessMonster posted:When using the for loops I keep getting the same error, no matter what I try. Tigren's solution is the better one IMO, but according to the docs (https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute), if you want to use dictionaries, you have to use the named parameters style instead of the question marks one. The question marks style is used when you have a list or tuple with the parameters already in order, for example: Python code:
Python code:
Space Kablooey fucked around with this message at 16:00 on Sep 22, 2016 |
# ? Sep 22, 2016 15:56 |
|
...and if you want to do it the other way, you need to group the values into a list rather than including them all as function arguments directly.Python code:
|
# ? Sep 22, 2016 16:13 |
|
Thanks for all the feedback. I managed to get it working with Tigrens way. The code is now looking like this: Python code:
For now it's still a lot of trial and error, and thinking about what I'm doing wrong but when I started a few weeks back I knew nothing at all (compared to you guys I guess that's still the case) but now I seem to find my way a bit. LochNessMonster fucked around with this message at 17:26 on Sep 22, 2016 |
# ? Sep 22, 2016 17:19 |
|
LochNessMonster posted:Thanks for all the feedback. I managed to get it working with Tigrens way. The code is now looking like this: Congrats, feels good doesn't it? Now read through these comics and check them off one by one when they apply to you. They will all apply to you eventually. https://medium.freecodecamp.com/coding-explained-in-25-profound-comics-8847ea03819c#.lzka3j5rh Tigren fucked around with this message at 17:55 on Sep 22, 2016 |
# ? Sep 22, 2016 17:48 |
|
Tigren posted:Congrats, feels good doesn't it? Now read through these comics and check them off one by one when they apply to you. They will all apply to you eventually. I like how #12 "identifying if picture is of a bird" went from "virtually impossible" to pretty doable nowadays.
|
# ? Sep 22, 2016 18:19 |
LochNess, glad to see you're enjoying python. It is by far my favorite language. As a small piece of advice, try and avoid the construct code:
Good luck with your coding!
|
|
# ? Sep 22, 2016 18:50 |
|
Eela6 posted:LochNess, glad to see you're enjoying python. It is by far my favorite language. Thanks for the feedback, could you explain what is actually wrong and how I could improve on that? I thought I'd catch the errors for that specific code and print it to stdout. That way I knew what went wrong.
|
# ? Sep 23, 2016 12:28 |
|
LochNessMonster posted:Thanks for the feedback, could you explain what is actually wrong and how I could improve on that? I thought I'd catch the errors for that specific code and print it to stdout. That way I knew what went wrong.
The first thing is a bit of dated advice. You can skip to the explanation of the second thing if you don't care about Python lore. Before Python 2.5, all exceptions inherited from Exception. Some of the exceptions that inherited from it included SystemExit and KeyboardInterrupt. That meant that you could conceivably catch and ignore some very fundamental built-in exceptions for exiting your Python program. Imagine hitting Control-C and having your exception handler print that out but not actually exit the program. In Python 2.5, they made a BaseException and made key system-internal exceptions inherit from that rather than from Exception. except Exception: will no longer catch these critical exceptions. The second thing is to be careful about catching everything of type Exception. In particular, Python uses exceptions internally for normal iteration behavior. See https://docs.python.org/2.7/library/stdtypes.html#iterator.next. There are a few other potential gotchas, but it's not going to destroy your program like it would in Python 2.4. That said, the third thing is to be specific about exceptions that you want to catch. In this case, you should use except sqlite3.Error: (https://docs.python.org/3/library/sqlite3.html?highlight=cursor#sqlite3.Error). This will catch all sqlite errors but allow the rest to bubble up out of your program. If you can identify a few very specific exceptions that you want to handle in the same way, it's even better to explicitly name them rather than catch some parent class of theirs. See http://stackoverflow.com/questions/6470428/catch-multiple-exceptions-in-one-line-except-block#6470452. The best practice is to be explicit and specific unless you have a very good reason not to be.
|
# ? Sep 23, 2016 13:36 |
|
The reason not to catch every exception is that if something goes terribly wrong, why continue? Specifically if you get an error connecting to the database, why then try to write something to it anyway? Clearly it's not going to work. You're just going to bury the useful exception message (the first one) under some other exceptions that you are sure to get if you continue to run your program. You should catch an exception whenever there's a specific exception that you know you're able to handle and continue to run the program. For example maybe getDealerId['id'] returns a KeyError and you decide to handle that by writing in a default ID (note that there are still better ways to get this behavior). I can't think of anything that could go wrong connecting to the database and still have your program run correctly the rest of the way. Finally, you say you'd print it to stdout so you know what's wrong. Well, usually the message will go to stderr, so unless you have some weird setup you should be able to see the message without needing to catch and print it.
|
# ? Sep 23, 2016 15:12 |
|
Reading these explenations and those links clears things up a bit (or a lot actually). The reason I initially did it this way was a) because I didn't know any better, and b) I had no idea which other (specific) Errors I was looking for. The reason I put it in there, was because my program seem to get all the input and put it in dictionary and added each dictionary to the list. But it didn't write the data to the database. This actually happened due to several reasons, some of which I found out with except Exception. Reasons were: database was in a different path than my program was looking in, table had a typo so couldn't insert and finally I didn't commit the changes. What I didn't realize is that I could possibly catch lots of other Exceptions. So I'm going to rewrite this with trying to catch a sqlite3.DatabaseError for the connection, and a sqlite3.IntegrityError to see if my insert is working properly (if it isn't, I don't want to pollute the database).
|
# ? Sep 23, 2016 16:01 |
Awesome. The posters above explained it better than I could have. As a quick note, if you're trying to debug, there's nothing wrong with putting off generic errors long enough to get some info first, I.e: try: // some problematic code except exception as err: // some additional debugging code that provides you info about the error raise err Later on you'll probably want to factor that out, but it can help you understand what's going on when your code behaves unexpectedly. Eela6 fucked around with this message at 17:37 on Sep 23, 2016 |
|
# ? Sep 23, 2016 16:07 |
|
I'll just add that if you're only printing out the error message, you're not getting any more information than if you had just let it hit. Eela6's suggestion is a good reason to catch exceptions while debugging but for it to provide benefits you need to print out some other useful information after catching. E.g. the value of some variables that may be related to the error. Finally, as in Eela6's example, you should still re-raise the exception if you're only catching it for debugging purposes (that is, if you don't do some error-handling that will allow your program to continue regardless). I'll also add that I usually re-raise an exception with a bare "raise" rather than "raise err". I think this provides a better traceback, but I was not able to confirm this with a quick Google.
|
# ? Sep 23, 2016 17:02 |
|
SurgicalOntologist posted:I'll just add that if you're only printing out the error message, you're not getting any more information than if you had just let it hit. Eela6's suggestion is a good reason to catch exceptions while debugging but for it to provide benefits you need to print out some other useful information after catching. E.g. the value of some variables that may be related to the error. Finally, as in Eela6's example, you should still re-raise the exception if you're only catching it for debugging purposes (that is, if you don't do some error-handling that will allow your program to continue regardless). I still have to wrap my head around the raising and reraising you guys keep mentioning. I did print out a lot of other variables (and sometimes type(var) for the bs4 variables) tp get an idea what was going wrong. I cleaned that part up before posting though.
|
# ? Sep 23, 2016 17:12 |
|
Oh, in that case catching the error does make some sense. Just add "raise" after though and you'll get more consistent behavior ("crashing" instead of chugging along like nothing is wrong).
|
# ? Sep 23, 2016 17:16 |
|
Yeah, exceptions are there to tell you something went wrong, and give you information about exactly what happened. They should tell you which line in your code caused it, and what type of error it is. Not catching them means your program crashes (bad), because there's a serious problem (bad) which needs to be fixed (good!) Catching exceptions is what you do when there are specific problems you want to handle. Things like I/O or network operations can be unreliable, there's a chance they could fail (throwing an exception) and you want your program to handle those situations gracefully. Catching those specific exceptions, in the places you expect they could happen, allows you to catch those errors and do whatever you need to do You just need to be careful of the overlap here, the exceptions you expect and the ones you don't. You don't want to silently catch the errors that point to a bug in your program, you need it to let you know that Bad Thing has gone wrong. That's why it's best to limit your catching to the specific types of exceptions you expect, and only have your try block cover the places where you expect it to occur (like where you make a call to write to disk). That way you reduce the chances of catching and swallowing something you didn't mean to, and losing valuable information Reraising is basically throwing an exception again - it's like saying "hang on a minute" with the catch, doing something, then saying "ok carry on crashing". Sometimes this is exactly what you want, or maybe something further back needs to see the exception too, and do some handling itself. Once you get the idea of exceptions in general, you should have a better idea of when you might want to do this kind of thing
|
# ? Sep 23, 2016 17:44 |
quote:
unnamed input Python code:
Python code:
Python code:
Python code:
Eela6 fucked around with this message at 22:07 on Sep 23, 2016 |
|
# ? Sep 23, 2016 17:48 |
|
SurgicalOntologist posted:The reason not to catch every exception is that if something goes terribly wrong, why continue? Specifically if you get an error connecting to the database, why then try to write something to it anyway? Clearly it's not going to work. You're just going to bury the useful exception message (the first one) under some other exceptions that you are sure to get if you continue to run your program. You can raise caught exceptions. So maybe I want to print something specific anytime an exception is caught, even if the raised exception wasn't expected, and then raise the exception again E: well gently caress this was talked about two posts later
|
# ? Sep 24, 2016 03:57 |
|
This code:code:
sometimes gets this error: quote:File "/storage/emulated/0/com.hipipal.qpyplus/scripts/.last_tmp.py", line 169, in <module> I've tried using f.write(s.encode("utf-8")) and f.write(str(s)) instead but that doesn't alleviate the problem.
|
# ? Sep 27, 2016 17:38 |
|
|
# ? May 8, 2024 05:27 |
Are you in python 2 or python 3?
|
|
# ? Sep 27, 2016 18:10 |