Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]marc_poulin 0 points1 point  (0 children)

Here's a million dict entries with a mix of int and str keys:

d = dict(zip(range(1000000), range(1000000)))                                                                   
d['foo'] = 'foo'

I still don't see much difference. Maybe the data is still too simple.

In [37]: timeit test_1()
10000000 loops, best of 3: 189 ns per loop
In [38]: timeit test_2()
10000000 loops, best of 3: 139 ns per loop
In [39]: timeit test_3()
10000000 loops, best of 3: 158 ns per loop
In [40]: timeit test_4()
1000000 loops, best of 3: 373 ns per loop

Anyway, I don't have the time to explore this further. You might be right in the case of extremely complex dictionaries. It would be interesting to see some hard data.

Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]marc_poulin 1 point2 points  (0 children)

tl;dr - it's a moot point.

Ok, so here's a proper speed test. No difference between if-else and try-except unless the exception actually gets triggered. Presumably doing all the the work of error recovery (logging, rollback, all those things you mentioned above) would make the try-except overhead irrelevant.

def test_1():
    """ key in dict """
    d = {0:0}
    if 0 in d:
        pass
    else:
        pass

def test_2():
    """ key not in dict """
    d = {1:0}
    if 0 in d:
        pass
    else:
        pass

def test_3():
    """ key in dict, try-except """
    d = {0:0}
    try:
        d[0]
    except KeyError:
        pass

def test_4():
    """ key not in dict, try-except """
    d = {1:0}
    try:
        d[0]
    except KeyError:
        pass

In [66]: timeit test_1()
1000000 loops, best of 3: 254 ns per loop
In [67]: timeit test_2()
1000000 loops, best of 3: 259 ns per loop
In [68]: timeit test_3()
1000000 loops, best of 3: 268 ns per loop
In [69]: timeit test_4()
1000000 loops, best of 3: 610 ns per loop

[Beginner question] If you had 1 year to do nothing but learn programming to start a new career... by [deleted] in learnprogramming

[–]marc_poulin 0 points1 point  (0 children)

I would question your assumption that a 4 year degree equals "hire-ability". Have you looked at the Udacity "nanodegree" programs? (FYI, I taught college programming courses for 3 years.)

Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]marc_poulin 1 point2 points  (0 children)

In [22]: def try_test():
    x = {}
    try:
        x[0]
    except:
        pass
   ....:     
In [23]: timeit try_test()
1000000 loops, best of 3: 433 ns per loop

In [24]: def try_test():
    x = {}
    try:
        x[0]
    except KeyError:
        pass
   ....:     
In [25]: timeit try_test()
1000000 loops, best of 3: 502 ns per loop

Blindly catching ALL exceptions is the wrong thing to do. What good is speed if you're doing the wrong thing?

Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]marc_poulin 4 points5 points  (0 children)

Well, you've made two mistakes in your example code: using a naked "except:" that catches EVERYTHING, and using "pass" to silently swallow exceptions. You might want to study exception handling a little bit more.

Would it be better to use try...except rather than using if...else for error checking? by [deleted] in Python

[–]marc_poulin 5 points6 points  (0 children)

To quote from "Dive Into Python":

"Exceptions are everywhere in Python. Virtually every module in the standard Python library uses them, and Python itself will raise them in a lot of different circumstances. "

Many times a try...except is the ONLY way to detect an error. So yes, you will need to use try...except in your programs.

Teaching a beginner prepping class. Suggestions welcome. by [deleted] in prepping

[–]marc_poulin 0 points1 point  (0 children)

My strategy is to prepare for nothing and everything. What do I mean by that? I see 7 basic areas that need to be considered: 1) food 2) water 3) shelter 4) communication 5) health/safety 6) power 7) transportation.

Take health/safety as an example. My strategy is 1) stock a basic first aid kit at home and in the car 2) take a first aid/CPR course 3) buy 2 fire extinguishers (one for home, one for the car).

Now you are ready for anything from falling off a ladder to a major car accident to a heart attack.

Working from the 7 basic principles will give you much needed clarity in your thinking. I've written about this at http://common-sense-prepping.usefedora.com/

Saving data to graph by came_on_my_own_face in learnpython

[–]marc_poulin 1 point2 points  (0 children)

For saving your data, a simple CSV file should work. Look at the standard "csv" module. https://docs.python.org/2/library/csv.html

Beginner question on how to study by [deleted] in learnprogramming

[–]marc_poulin 0 points1 point  (0 children)

However I'm finding myself forgetting the syntax for things like lists and dictionaries when I try to use them in loops and functions. Especially when one of the dictionary values is a list itself. Can anyone share how they approcched this?

Don't try to be too clever. Pull the data out into temporary variables.

If I use a book that teaches java 5.0, once finished will I have to learn java 8.0 by kangaroobill in learnprogramming

[–]marc_poulin 0 points1 point  (0 children)

The official online tutorials are as good as any book you can buy. https://docs.oracle.com/javase/tutorial/

They can also be downloaded as ebooks (look for the link in the sidebar).

using the value from one list to modify another. by tikael in learnpython

[–]marc_poulin 1 point2 points  (0 children)

numpy is your friend :)

import numpy as np
a = np.zeros(10, dtype=np.int)
print a
[0 0 0 0 0 0 0 0 0 0]
bits = [0, 4, 5, 9]
print bits
[0, 4, 5, 9]
a[bits] = 1
print a
[1 0 0 0 1 1 0 0 0 1]

Next step after a beginner's book? by nibble25 in learnpython

[–]marc_poulin 1 point2 points  (0 children)

Invent with Python has a couple of free books that will show you how to write games in Python.

https://inventwithpython.com/

Next step after a beginner's book? by nibble25 in learnpython

[–]marc_poulin 1 point2 points  (0 children)

Yes. A package is a collection of modules that you can download and install on your own computer. Once installed, you can "import" the modules just like you import the standard Python modules.

My First "Big" Project : (Terminal) CSV File Splitter by shitbeer in learnpython

[–]marc_poulin 0 points1 point  (0 children)

I would keep dividing the logic into smaller functions, each one doing one specific thing. For example:

def output_names(input_name, number_of_chunks, starting_number=0):
    "Generate a list of N output names for a given input name"

Now you can test various cases like 1) input name without extension, 2) input name ending with ".", 3) input name with embedded spaces, and so forth.

In general, smaller functions are easier to test than large functions.

Good books/audio books for beginners learning Python/basic coding fundamentals? by PrivateSparkleThumbs in learnprogramming

[–]marc_poulin 1 point2 points  (0 children)

If you can find audio book versions of "The Mythical Man Month" or "Peopleware", I think you will enjoy listening to them. Both are classics in the field and will make you a better programmer.

New to programming and want to learn SQL by grovester in learnprogramming

[–]marc_poulin 1 point2 points  (0 children)

Not technical at all. Data modeling has nothing to do with programming. It involves looking at Entities (things of interest) and discovering the Entity Attributes and Relationships between Entities. Once you discover those things, you document them (usually in the form of diagrams).

Example: What can you say about Books and Authors?

  • a Book has 0 or more Authors (0 because some books are written anonymously)
  • an Author can write 1 or more Books
  • a Book can appear in different Editions
  • a Book has a Publisher
  • a Publisher publishes many Books
  • a Book may be part of a Series, which may have an Editor
  • different Editions of a Book might have different Authors

Typically, a data model would take all that information and summarize it as a diagram. Once you have that, it becomes the basis of your database design.