Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
EAT THE EGGS RICOLA
May 29, 2008

hooah posted:

Any idea why, after debugging once, PyCharm wants to always debug, even though I'm hitting the "run" hotkey?

Check which hotkey you're using, there's one for run and one for "do whichever thing I did last again".

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell


Did this get good in the past year? In the past this thread has always been quite negative on diveintopython.

The two recommendations of this thread have always been Learn Python the Hard Way and Think Python.

Of course, those are aimed at a beginner. If someone is familiar with programming, I'm not sure what the best resource would be...

Hed
Mar 31, 2004

Fun Shoe

Thermopyle posted:

Did this get good in the past year? In the past this thread has always been quite negative on diveintopython.

The two recommendations of this thread have always been Learn Python the Hard Way and Think Python.

Of course, those are aimed at a beginner. If someone is familiar with programming, I'm not sure what the best resource would be...

Yeah I kind of recall someone I respected disparaging "the Hard Way" on here. I share your thoughts at the end though, I was thinking at the very least a person familiar with perl could skim the sections on data structures and functions to get started, then pick up the other pieces as they use them.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

salisbury shake posted:

You're calling logging.info and not info on the instance of your logger. The name of the logger needs to be in the scope of the function that needs to use it. There are different ways to go about this. I used a module level logger that I imported in different components in my last project. You can name it at the top of your file, make it an class attribute, etc.

Ok I fixed that but it seems like the location of the "from myproj.utils import execute" is still important because when I have it at the top of the file (before it hits my setup_logging method) then myproj.utils doesn't seem to get the modified logging config.

hooah
Feb 6, 2006
WTF?

EAT THE EGGS RICOLA posted:

Check which hotkey you're using, there's one for run and one for "do whichever thing I did last again".



Yup, that was it. Apparently whoever made the Visual Studio-like keybindings bound ctrl+f5 to two different commands. Good job, folks.

accipter
Sep 12, 2003

EAT THE EGGS RICOLA posted:

Check which hotkey you're using, there's one for run and one for "do whichever thing I did last again".



Hey that's pretty handy! For those of you wondering, you can get to that menu from "Help->Find Action..." or Ctrl+Shift+A (depending on your shortcuts).

accipter
Sep 12, 2003

Thermopyle posted:

Did this get good in the past year? In the past this thread has always been quite negative on diveintopython.

The two recommendations of this thread have always been Learn Python the Hard Way and Think Python.

Of course, those are aimed at a beginner. If someone is familiar with programming, I'm not sure what the best resource would be...

I remember some people having negative opinions of Dive Into Python as well, but I couldn't find anything well I was work. My problem with Learn Python the Hard Way is that it is specific to Python 2, and I think it is about time that Python3 be the standard. In the end, I recommended the tutorial from the official documentation.

EAT THE EGGS RICOLA
May 29, 2008

accipter posted:

Hey that's pretty handy! For those of you wondering, you can get to that menu from "Help->Find Action..." or Ctrl+Shift+A (depending on your shortcuts).

Oh, yeah, this feature owns. You can look up any action with ctrl-shift-A followed by typing in any command.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
What is a simple way to parse information out of an HTML document? My work has an application that renders forms using HTML but it's not a website. I want to run through a file and any input element it sees, it should record the type, name and value. I'm assuming I don't want to use regex to parse html...

vikingstrike
Sep 23, 2007

whats happening, captain

Hughmoris posted:

What is a simple way to parse information out of an HTML document? My work has an application that renders forms using HTML but it's not a website. I want to run through a file and any input element it sees, it should record the type, name and value. I'm assuming I don't want to use regex to parse html...

There are libraries like BeautifulSoup that will help parse HTML for you. I've used BeautifulSoup in the past and it was straightforward to pickup.

EAT THE EGGS RICOLA
May 29, 2008

Hughmoris posted:

What is a simple way to parse information out of an HTML document? My work has an application that renders forms using HTML but it's not a website. I want to run through a file and any input element it sees, it should record the type, name and value. I'm assuming I don't want to use regex to parse html...

BeautifulSoup is nice.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Hughmoris posted:

What is a simple way to parse information out of an HTML document? My work has an application that renders forms using HTML but it's not a website. I want to run through a file and any input element it sees, it should record the type, name and value. I'm assuming I don't want to use regex to parse html...

This should be a piece of cake with bs4: http://www.crummy.com/software/BeautifulSoup/bs4/doc/

edit: drat you guys are fast

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Wow, thanks. Looks like BeautifulSoup is what I'll try. I looked at it months ago and was a little overwhelmed but I've learned a little HTML since then so hopefully this time I'll make some progress.

accipter
Sep 12, 2003

Hughmoris posted:

What is a simple way to parse information out of an HTML document? My work has an application that renders forms using HTML but it's not a website. I want to run through a file and any input element it sees, it should record the type, name and value. I'm assuming I don't want to use regex to parse html...

You might also want to consider lxml.html.

Gothmog1065
May 14, 2009
Maybe I should wait a bit to learn how to do python like this, but I'm trying to do things in as little code as possible. an example is from way back in the thread, trying to do something similar to this.

Dominoes posted:

Putting it all together:
Python code:
weights = {"homework" :  .1, "quizzes" : .3, "tests" : .6}

def get_average(student):
    return sum(student[item] * weight for item, weight in weights.items())

Still doing the code academy stuff and it's on the "practice by yourself" and I'm doing a really incredibly simple "add the numbers of an integer" type function. The "answer" they are looking for is something like this:
code:
def digit_sum(n):
result = 0
for nums in str(n):
    result += int(nums)
return result
I'm trying to get that for statement, if possible down to a single line like the example given above. I guess I'm trying to learn how to build that inline for statement, or is it even necessary in this situation?

duck monster
Dec 15, 2004

Just found an old game I wrote in about 2006-2007 and it has super calls like this:
code:
class Thingo(sprite):
    def __init__(self,parameter):
           <some stuff>
           sprite.__init__(self,parameter)
Which still works. From memory it might have been the old way of calling super methods?

Any idea why its not done that way anymore? It strikes me as a less boilercode-ish way of calling super.

EAT THE EGGS RICOLA
May 29, 2008

Gothmog1065 posted:

Maybe I should wait a bit to learn how to do python like this, but I'm trying to do things in as little code as possible. an example is from way back in the thread, trying to do something similar to this.


Still doing the code academy stuff and it's on the "practice by yourself" and I'm doing a really incredibly simple "add the numbers of an integer" type function. The "answer" they are looking for is something like this:
code:
def digit_sum(n):
result = 0
for nums in str(n):
    result += int(nums)
return result
I'm trying to get that for statement, if possible down to a single line like the example given above. I guess I'm trying to learn how to build that inline for statement, or is it even necessary in this situation?

The first example is okay because it's pretty clear, but you shouldn't be trying to write clever code, you should be trying to write code that is as clear as possible for someone else (including you in the future) to read. Following pep8 helps a lot with this.

Gothmog1065
May 14, 2009

EAT THE EGGS RICOLA posted:

The first example is okay because it's pretty clear, but you shouldn't be trying to write clever code, you should be trying to write code that is as clear as possible for someone else (including you in the future) to read. Following pep8 helps a lot with this.

Alright. That's kind of what I was looking for in an answer, I'll move on!

SurgicalOntologist
Jun 17, 2004

Gothmog1065 posted:

Still doing the code academy stuff and it's on the "practice by yourself" and I'm doing a really incredibly simple "add the numbers of an integer" type function. The "answer" they are looking for is something like this:
code:
def digit_sum(n):
result = 0
for nums in str(n):
    result += int(nums)
return result
I'm trying to get that for statement, if possible down to a single line like the example given above. I guess I'm trying to learn how to build that inline for statement, or is it even necessary in this situation?

First, identify that that code pattern is a sum.
Python code:
def digit_sum(n):
   return sum(           )
Next, stick the loopy part in there.
Python code:
def digit_sum(n):
    return sum(         for digit in str(n))
Then, what is the part you want to actually add for each time through the loop?
Python code:
def digit_sum(n):
    return sum(int(digit) for digit in str(n))
There you go; that's the equivalent of your original code. If that line of thought doesn't make any sense, be patient, code academy will get to it.

cliffy
Apr 12, 2002

Gothmog1065 posted:

Alright. That's kind of what I was looking for in an answer, I'll move on!

While generally what EGGS wrote is true, you shouldn't think that using sum and a list comprehension together to write something in one line as being too clever. As long as you're familiar with those constructs, then the code is entirely and clearly legible. Even if you're not, the syntax is so clear that you should be able to figure out what's going on.

If you're ever writing code you think may be hard to read, then you should try to make it easier to read. Usually by naming methods and variables appropriately, and adding a comment or two explaining what's going on. Don't stop yourself from using clearly defined and understood language constructs just to avoid being 'clever'.

EAT THE EGGS RICOLA
May 29, 2008

cliffy posted:

While generally what EGGS wrote is true, you shouldn't think that using sum and a list comprehension together to write something in one line as being too clever. As long as you're familiar with those constructs, then the code is entirely and clearly legible. Even if you're not, the syntax is so clear that you should be able to figure out what's going on.

If you're ever writing code you think may be hard to read, then you should try to make it easier to read. Usually by naming methods and variables appropriately, and adding a comment or two explaining what's going on. Don't stop yourself from using clearly defined and understood language constructs just to avoid being 'clever'.

Yeah, the two examples you mentioned are totally fine in terms of clarity when they use list comprehensions, but it's best to keep readability in mind before you start doing that with significantly more complicated code.

Gothmog1065
May 14, 2009

SurgicalOntologist posted:


Python code:
def digit_sum(n):
    return sum(int(digit) for digit in str(n))
There you go; that's the equivalent of your original code. If that line of thought doesn't make any sense, be patient, code academy will get to it.

Goddammit, I had the int() in the wrong place.

Thanks guys, I kind of get excited and try to learn things ahead, which sometimes is a good thing, and sometimes bad.

Dominoes
Sep 20, 2007

Surgical Ont answered your question, but since it's academic, two more things to think about :

This accomplishes the same thing and is about as efficient, but is frowned upon (including by Guido) for being more difficult to read:
Python code:
def digit_sum(n):
    return sum(map(int, str(n)))
Your approach of converting the original number to a string, then integers is intuitive, but slow. The accepted answer to this post shows how to solve the problem numerically.

Dominoes fucked around with this message at 22:39 on Mar 19, 2015

cliffy
Apr 12, 2002

Just as an aside, learning how to effectively use the built-in functions map, reduce, and filter will make your code look and feel a lot simpler and cleaner.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

cliffy posted:

Just as an aside, learning how to effectively use the built-in functions map, reduce, and filter will make your code look and feel a lot simpler and cleaner.

This is true, with the stress heavily on effectively.

The majority of the time that I see those used they're just code golf. I mean, I don't know if that's true over all Python code, or if it's just true over the Python code I happen to read, but they're easy to abuse.

SurgicalOntologist
Jun 17, 2004

Or you could go full functional style.

Python code:
from toolz import pipe
from toolz.curried import map

def digit_sum(n):
    return pipe(n,
        str,
        map(int),
        sum,
    )
I find it a more readable version of Domino's version (i.e. using map). But I know I'll now get admonished for writing Python in a functional style. :shrug: In this case it's not much of an improvement, but with more complex routines it really makes you think about the operations in an effective way (for me at least).

Fergus Mac Roich
Nov 5, 2008

Soiled Meat
What's wrong with using a functional style? I don't know that much about it but from hacking around with SICP/Scheme it seems pretty intuitive(moreso than a procedural style, honestly), and it looks like the tools are there to do it in Python.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Fergus Mac Roich posted:

What's wrong with using a functional style?

Nothing.

Cingulate
Oct 23, 2012

by Fluffdaddy
What would the original, list-comprehension one be called? What ”style" is that?

(My Python is like 75% list comprehensions these days.)

SurgicalOntologist
Jun 17, 2004

The only thing "wrong" with a functional style is it's not mainstream Python, so your typical Python programmer is not likely to have encountered it and will be confused.

Cingulate, I don't know about identifying a style (it's not really clear cut, even my version isn't functional in the more academic sense), but it's worth pointing out that there's no list comprehension, but a generator expression. You shouldn't have 75% list comprehensions, but a lot of generator expressions/comprehensions of all kinds is usually considered the best way to write Python code.

Dominoes
Sep 20, 2007

SurgicalOntologist posted:

Or you could go full functional style.

Python code:
from toolz import pipe
from toolz.curried import map

def digit_sum(n):
    return pipe(n,
        str,
        map(int),
        sum,
    )
I find it a more readable version of Domino's version (i.e. using map). But I know I'll now get admonished for writing Python in a functional style. :shrug: In this case it's not much of an improvement, but with more complex routines it really makes you think about the operations in an effective way (for me at least).

Alternatively -

Python code:
from functools import reduce

def digit_sum(n):
    n = str(n)
    reduce(lambda num, a: int(a) + int(num), n)
Python code:
def digit_sum(n):
    n = str(n)
    if len(n) == 1:
        return n
    return int(n[0]) + int(digit_sum(n[1:]))

Dominoes fucked around with this message at 22:34 on Mar 19, 2015

duck monster
Dec 15, 2004

I tend to use map, reduce and filter quite a bit, but honestly it has a tendency to make the code harder to explain other than "this is the effect, but how it works might take some time".

Be careful with it, good python is about being able to just glance at the code and see how it works. That is not always the case with some of the functional primitives in python.

BigRedDot
Mar 6, 2008

I haven't used map or filter in years. List comprehensions and generator expressions
  • are declarative (almost always the best option when it is available)
  • are non-trivially more efficient (dispatch immediately to Python C API)
As for the style, it's sometimes referred to as "declarative". In the manner of Prolog, the idea is to tell the computer, directly, what you want, rather than a bunch of steps for how to do it. The implication being that you always have to know what you want, but that giving the steps requires converting what you want into the sequence of steps, and that eliminating this conversion process leaves less room for errors.

BigRedDot fucked around with this message at 03:18 on Mar 20, 2015

Harriet Carker
Jun 2, 2009

Beating my head against a wall here (as usual).

In IDLE, when I do this:

code:
x = [[0,0], [0,0]]
x[0][0] = "!"
x outputs [["!", 0], [0,0]] as expected. However, when I run this code, using any 3x3 matrix as data (such as [[1,2,3], [4,5,6], [7,8,9]])

code:
def checkio(data):
    rows = len(data)
    columns = len(data[0])
    res = list()
    new_row = list()

    #create empty translated matrix
    for row in range(rows):
        new_row.append(0)
    for column in range(columns):
        res.append(new_row)

    res[0][0] = "!"
    return res
res outputs [['!', 0, 0], ['!', 0, 0], ['!', 0, 0]]

I can't for the life of my figure out why res[1][0] and res[2][0] are getting changed as well. Working on a matrix translation problem, and eventually I will add the code

code:
    #swap i and j positions    
    for row_number, row in enumerate(data):
        for column_number, element in enumerate(row):
            res[column_number][row_number] = element
once I find out what's causing this unexpected behavior.

KaneTW
Dec 2, 2011

You aren't copying the list.

Harriet Carker
Jun 2, 2009

KaneTW posted:

You aren't copying the list.

I'm afraid I don't understand. Could you provide a little more detail?

BabyFur Denny
Mar 18, 2003
Your matrix consists of three times the "new_row" object. If you change the first element of "new_row" to a "!", of course it will show up in all three rows.
res.append(list(new_row)) should work better since you are creating a new copy of the list each time instead of adding the same object.

KaneTW
Dec 2, 2011

Lists in Python aren't copied by default, so
code:
 a = [0, 1]
b = a
a[0] = 1
implies b[0] == 1. You need to create a copy of new_row by new_row[:] or list(new_row) before appending. Note that this only goes one nesting level deep.

Consider using an existing matrix library.

Nippashish
Nov 2, 2005

Let me see you dance!
You should use numpy if you're going to work with matrices.

Adbot
ADBOT LOVES YOU

Harriet Carker
Jun 2, 2009

Ahh thank you! I feel silly now.


I'm just doing some practice problems on CheckIO but thanks for the suggestions about working further with matrices.

  • Locked thread