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
Fergus Mac Roich
Nov 5, 2008

Soiled Meat

The Royal Scrub posted:

Is there a way to set variable type once and be done? Designating a variable as int(input(variable),10) every time I redefine it doesn't seem right.

If I don't do that it wants it to be a string, I guess because I'm using input but yeah.

Python uses type inference, so you aren't declaring a variable to be a specific type. What you're doing is using the int() built-in function to parse the result of the input() function(which is a string, because that's what the function returns) into an int. int() returns an int, so the variable is assigned an int, that's all. This is conceptually like using C's atoi(), and ultimately, any time you get information from standard input, you must do something like this. I believe that this is the case irrespective of the language you're using; as you can see, even C must do the same thing(though it provides a facility to make this case more convenient in the form of scanf()).

Fergus Mac Roich fucked around with this message at 05:00 on Dec 16, 2015

Adbot
ADBOT LOVES YOU

salisbury shake
Dec 27, 2011

The Royal Scrub posted:

Is there a way to set variable type once and be done? Designating a variable as int(input(variable),10) every time I redefine it doesn't seem right.

If I don't do that it wants it to be a string, I guess because I'm using input but yeah.

Names are ambiguous references to any kind of Python object. input() returns a string.

Even in a statically typed language:
code:
String s = "12345678";
int n = s;
Would hopefully throw some warnings.

Python code:
def parse_int(line: str, base: int=10) -> int:
    if line.isdigit():
       return int(line, base)

    raise ValueError("%s not an int." % line)

some_name = parse_int(input(prompt))
Might be cleaner?

e: base isn't length

salisbury shake fucked around with this message at 05:15 on Dec 16, 2015

SurgicalOntologist
Jun 17, 2004

That's going to fail silently if something goes wrong. I would just do

Python code:
def get_int_from_user(prompt):
    return int(input(prompt))
and catch exceptions in the caller if necessary.

accipter
Sep 12, 2003

The Royal Scrub posted:

Is there a way to set variable type once and be done? Designating a variable as int(input(variable),10) every time I redefine it doesn't seem right.

If I don't do that it wants it to be a string, I guess because I'm using input but yeah.

No. Make a function that does that:

Python code:
def ask_for_int(prompt):
    return int(raw_input(prompt), 10)

Dominoes
Sep 20, 2007

Is there a way to make Pycharm stop weak-warning uppercase variable names? Unlike some inspections, the only alt-enter option is to rename the variable. I can't find a suitable error code to ignore on this page. I can disable the pep8 naming convention inspection, but that's broader than I'd like.

Dominoes fucked around with this message at 13:12 on Dec 26, 2015

Feral Integral
Jun 6, 2006

YOSPOS

Hey quick ipdb question: is there a way to load from a python file while in the debugger? In my case I've ipdb.set_trace()'d into my app, but now I want to load some utilty functions into the namespace. If I was in the interpreter I would just use %run or %load. What's the best way to do this?

Thermopyle
Jul 1, 2003

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

Feral Integral posted:

Hey quick ipdb question: is there a way to load from a python file while in the debugger? In my case I've ipdb.set_trace()'d into my app, but now I want to load some utilty functions into the namespace. If I was in the interpreter I would just use %run or %load. What's the best way to do this?

I've never really investigated this, I just do something like:

Python code:
if error:
    import my_utilities
    ipdb.set_trace()

Feral Integral
Jun 6, 2006

YOSPOS

Thermopyle posted:

I've never really investigated this, I just do something like:

Python code:
if error:
    import my_utilities
    ipdb.set_trace()

Oh, that seems...obvious. Haha thanks!

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

Dominoes posted:

Is there a way to make Pycharm stop weak-warning uppercase variable names? Unlike some inspections, the only alt-enter option is to rename the variable. I can't find a suitable error code to ignore on this page. I can disable the pep8 naming convention inspection, but that's broader than I'd like.

The easiest way, of course, is to use PEP8 compliant variable names.

Nippashish
Nov 2, 2005

Let me see you dance!

KernelSlanders posted:

The easiest way, of course, is to use PEP8 compliant variable names.

Sometimes pep8 is annoying.

Thermopyle
Jul 1, 2003

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

Nippashish posted:

Sometimes pep8 is annoying.

Agreed, but I find its usually better to just put up with it.

Dominoes
Sep 20, 2007

KernelSlanders posted:

The easiest way, of course, is to use PEP8 compliant variable names.
Scientific programming makes sense with uppercase variable names (including things like Δ) when convention calls for them. Popular libraries like scikit learn use them in documentation.

Nippashish posted:

Sometimes pep8 is annoying.
I use the 79-char line limit as a general guideline, but won't wrap if only going a bit past. PyCharm doesn't handle the whitespace-around-arithmetic operator check properly, so I disable that too.

QuarkJets
Sep 8, 2008

I would say to just disable all PEP8 naming conventions, although you might be able to define your own set of warning rules that include most of the naming conventions but not the ones that you don't want. Kind of a pain in the rear end and probably more trouble than it's worth.

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

Dominoes posted:

Scientific programming makes sense with uppercase variable names (including things like Δ) when convention calls for them. Popular libraries like scikit learn use them in documentation.

I'm not sure that arguing you want unicode variable names is much of an improvement.

I don't follow your point about sklearn. Are you saying that there is code in an sklearn docstring that doesn't follow sklearn coding standards?

Dominoes
Sep 20, 2007

QuarkJets posted:

I would say to just disable all PEP8 naming conventions.
I took that path.

KernelSlanders posted:

I'm not sure that arguing you want unicode variable names is much of an improvement.

I don't follow your point about sklearn. Are you saying that there is code in an sklearn docstring that doesn't follow sklearn coding standards?
Slearn uses uppercase X and C in their examples; filterpy's examples use uppercase letters for many parameters per convention; academic papers use variables with uppercase letters - there's value in transferring these intact; if I have a variable like Δt, changing it to δt doesn't have the same effect.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
idk writing simply dt is a super old convention in scientific programming, graphics programming, maybe even programming in general I'd say

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.

Symbolic Butt posted:

idk writing simply dt is a super old convention in scientific programming, graphics programming, maybe even programming in general I'd say

Yeah, changing to δt kind of misses the point. What's wrong with dt or deltaT?

Dominoes
Sep 20, 2007

KernelSlanders posted:

Yeah, changing to δt kind of misses the point. What's wrong with dt or deltaT?
Nothing. Your first example is pep-8 compliant and is also a common convention; your second would raise the same weak warning.

Dominoes fucked around with this message at 16:40 on Dec 31, 2015

KernelSlanders
May 27, 2013

Rogue operating systems on occasion spread lies and rumors about me.
Er... delta_t I mean. :bang:

Cingulate
Oct 23, 2012

by Fluffdaddy

KernelSlanders posted:

Yeah, changing to δt kind of misses the point.
That's one of the selling points of Julia.

QuarkJets
Sep 8, 2008

Cingulate posted:

That's one of the selling points of Julia.

Being able to use unicode characters in variable names sounds like a complete nightmare to me

Cingulate
Oct 23, 2012

by Fluffdaddy
You're probably looking at this from a software engineer standpoint. Julia, I think, is aimed at scientists-first who want to have their code look as much as math formulae as possible.

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS
Should be simple iterating through dictionaries question.Haven't worked with them until today.
I'm reading from a file. It reads a part of a line that denotes how many years a player has been playing.
It works fine.
code:
Y = {'2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '10': 0, '11': 0, '12': 0, '13': 0,
'14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0} #total appearances in file for year

#TWO other dicts called YAvg and YTot with same keys. 
The keys are the number of Years played and the values are how many times it showed up in the file.
Values of YTot are another number that is added up. I need to put the value of YTot['2'] / Y['2'] into YAvg ['2'] in a loop.

code:
for key, value in YAvg.iteritems():
	YAvg[value] = YTot[value] / Y[value]
which is not right. I'm just a little confused about how to do this. Please don't give me a solution that's extremely complicated and advanced to my apparent noob skills, I feel like it's just a matter of syntax.

ButtWolf fucked around with this message at 23:31 on Jan 1, 2016

Dominoes
Sep 20, 2007

You need to reference by the key, not value.

Python code:
y_avg = {}
for (year, count), count_tot in zip(y.items(), y_tot.values()):
    y_avg[year] = count_tot / count
Note: I've renamed your input dicts, and this will raise a ZeroDivisionError on your example data.

Dominoes fucked around with this message at 23:51 on Jan 1, 2016

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS

Dominoes posted:

You need to reference by the key, not value.

Python code:
y_avg = {}
for (year, count), count_tot in zip(y.items(), y_tot.values()):
    y_avg[year] = count_tot / count
Note: I've renamed your input dicts, and this will raise a ZeroDivisionError on your example data.

Why did you rename anything? Now there is y_avg - y - y_tot - count_tot ... I don't understand.
BTW: most of the data is not zeroes, but I'll set up an if !0 thingy.

Dominoes
Sep 20, 2007

The python style guide recommends using only lowercase letters in variable names, and using underscores to separate words.

I'm not clear how you're constructing the dicts, but I think the prob is you're accessing by values instead of keys.

Dominoes fucked around with this message at 00:00 on Jan 2, 2016

Cingulate
Oct 23, 2012

by Fluffdaddy

ButtWolf posted:

I need to put the value of YTot['2'] / Y['2'] into YAvg ['2']
How about

code:
yavg = {year:YTot[year] / Y[year] for year in Y}

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS

Cingulate posted:

How about

code:
yavg = {year:YTot[year] / Y[year] for year in Y}

:thumbsup:

Dominoes posted:

The python style guide recommends using only lowercase letters in variable names, and using underscores to separate words.

I'm not clear how you're constructing the dicts, but I think the prob is you're accessing by values instead of keys.

I don't care 'bout no naming conventions right now. This program is all bullshit for me anyway. Although thanks for the link, I'll read it eventually. Now I need to learn how to format when printing dictionaries to files. Uggh...
Anyone here have any experience webscraping?

Cingulate
Oct 23, 2012

by Fluffdaddy

ButtWolf posted:

:thumbsup:


I don't care 'bout no naming conventions right now. This program is all bullshit for me anyway. Although thanks for the link, I'll read it eventually. Now I need to learn how to format when printing dictionaries to files. Uggh...
Anyone here have any experience webscraping?
BeautifulSoup.

Also start paying attention to naming conventions, they're there for a reason.

QuarkJets
Sep 8, 2008

Cingulate posted:

You're probably looking at this from a software engineer standpoint. Julia, I think, is aimed at scientists-first who want to have their code look as much as math formulae as possible.

I am a scientist, by training and by profession, and this sounds like a terrible idea to me. I can't feel sorry enough for the first grad student who has to receive and modify legacy code with a bunch of unicode characters littered throughout it.

I would also wager that most scientists don't actually care about spending the extra time looking up unicode characters in order to have their code look like math formulae; I think that we mostly just want to code in a way that's easy and that executes quickly. Unless maybe LaTeX syntax for creating unicode characters is supported, I suppose

Dominoes
Sep 20, 2007

QuarkJets posted:

Unless maybe LaTeX syntax for creating unicode characters is supported, I suppose
Julia, Jupyter, and IPython syntax: backslash, then the name like 'pi', then tab.

QuarkJets
Sep 8, 2008

Dominoes posted:

Julia, Jupyter, and IPython syntax: backslash, then the name like 'pi', then tab.

That's LaTeX syntax and not what I was imagining, that's pretty cool then

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

Cingulate posted:

That's one of the selling points of Julia.

Python also allows a lot of characters outside of ASCII to be used in identifier names (but not all of them; they typically have to have the categorization "letter" in the Unicode database).

(No code tags because vBulletin mangles non-ASCII characters inside those)

quote:

from pprint import pprint
import numpy as np
r = np.random.randn(9).reshape((3, 3))
Δr = np.gradient(r)
pprint(r)
pprint(Δr)
code:
$ python delta.py 
array([[ 0.01040745,  1.05626835,  0.23928078],
       [-1.40756775, -0.67872115, -0.79094509],
       [ 1.7187268 , -0.45563915,  0.88105825]])
[array([[-1.4179752 , -1.7349895 , -1.03022587],
       [ 0.85415967, -0.75595375,  0.32088873],
       [ 3.12629454,  0.223082  ,  1.67200334]]),
 array([[ 1.0458609 ,  0.11443667, -0.81698757],
       [ 0.7288466 ,  0.30831133, -0.11222394],
       [-2.17436594, -0.41883427,  1.33669739]])]

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS
edit: Got it.

Anyone willing to help me tomorrow? I'm on the last part of my project and kind of burnt out. I have a program that reads a .csv and does some calculations based on the info.

I how have a text that I need to do the same thing with only it's formatted differently, and I'm having trouble.

email - sisyphean.0 at gmail

Shouldn't be too intensive, I'm just dumb.

ButtWolf fucked around with this message at 06:17 on Jan 4, 2016

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Lysidas posted:

Python also allows a lot of characters outside of ASCII to be used in identifier names (but not all of them; they typically have to have the categorization "letter" in the Unicode database).

(No code tags because vBulletin mangles non-ASCII characters inside those)

code:
$ python delta.py 
array([[ 0.01040745,  1.05626835,  0.23928078],
       [-1.40756775, -0.67872115, -0.79094509],
       [ 1.7187268 , -0.45563915,  0.88105825]])
[array([[-1.4179752 , -1.7349895 , -1.03022587],
       [ 0.85415967, -0.75595375,  0.32088873],
       [ 3.12629454,  0.223082  ,  1.67200334]]),
 array([[ 1.0458609 ,  0.11443667, -0.81698757],
       [ 0.7288466 ,  0.30831133, -0.11222394],
       [-2.17436594, -0.41883427,  1.33669739]])]

I just checked and nabla doesn't work because it's not a letter :qq:

BigRedDot
Mar 6, 2008

Hey new Bokeh time. https://goo.gl/rn9Dca :) Images link to http://demo.bokehplots.com

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS
If anyone has experience with beautifulsoup, I could use a little help.
I use a program to pull in some data

code:
per_game_line_2016 = soup.find("tr", id="per_game.2016")
HTML is like this:

code:
<tr data-row="2" class="full_table" id="per_game.2016">
   <td align="center">C</td>
   <td align="right">.581</td>
   <td align="right">0.0</td>
   <td align="right">0.0</td>
   <td align="right"></td>
   <td align="right">2.6</td>
   <td align="right">4.4</td>
   <td align="right">.581</td>
Everything inside <td>s gets written into a file, but I'm accessing each one like line[2] would be .581. This creates a problem when the data is blank, like in td4. If blank they should all be treated as .000 and would like to replace when it prints to the file, but I'm having trouble understanding parent child and sibling stuff.

I can just look at the file and manually add it, but I have a thousand or so lines to add, so I don't want to.

SurgicalOntologist
Jun 17, 2004

Can't you just use an if statement? Quick and dirty, something like:
Python code:
per_game_line_2016 = [float(td.text) if td.text else 0.0 for td in soup.find('tr', id='per_game.2016')]
Not sure why parent/sibling has to get into it.

ButtWolf
Dec 30, 2004

by Jeffrey of YOSPOS

SurgicalOntologist posted:

Can't you just use an if statement? Quick and dirty, something like:
Python code:
per_game_line_2016 = [float(td.text) if td.text else 0.0 for td in soup.find('tr', id='per_game.2016')]
Not sure why parent/sibling has to get into it.

Navigable string has no attribute 'text'

Adbot
ADBOT LOVES YOU

SurgicalOntologist
Jun 17, 2004

Right I've encountered that problem before. Better to do a second find then iterate over all the children. I don't know enough about HTML to know what those NavigableStrings are, but they get in the way. Something like this should filter them out.

Python code:
[float(td.text) if td.text else 0.0 for td in soup.find('tr', id='per_game.2016').find_all('td', attrs=dict(align='right'))]
So, instead of iterating over the tr element (which includes td elements and NavigableStrings), search it for td elements to get only those.

I added the attrs thing in there too so you don't get that "C" which I assume is the heading.

I'll also mention that pandas can read HTML into dataframes directory, so you might want to try that. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_html.html

Often something like
Python code:
import pandas as pd
pd.read_html(url)[0].to_csv(path)
is all you need. Though there are some tables that it can't find, and you may have to change the index if there are multiple tables on the page.

SurgicalOntologist fucked around with this message at 04:30 on Jan 8, 2016

  • Locked thread