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
LochNessMonster
Feb 3, 2005

I need about three fitty


Tigren posted:

Your string substitution is out of place.


dealerVehicles = query_db("select * from motorcycles where dealer = %s" % (dealer))


If you're using python3, take a look at .format() instead. https://pyformat.info/

I'll read up on that, thanks. I have done several tutorials and didn't come across string substitution yet, so I'v been googling that part. I'm indeed using python3 so that's probably my first issue.

I noticed you mentioned my for loop overwriting the variable with each iteration but changed your reply so it's gone. That was a relevant remark as well though, I'm kinda ashamed I didn't notice that myself.

Adbot
ADBOT LOVES YOU

VikingofRock
Aug 24, 2008




LochNessMonster posted:

I noticed you mentioned my for loop overwriting the variable with each iteration but changed your reply so it's gone. That was a relevant remark as well though, I'm kinda ashamed I didn't notice that myself.

Half the code I post on this site has some stupid mistake like that, so don't sweat it. It happens.

Tigren
Oct 3, 2003

LochNessMonster posted:

I'll read up on that, thanks. I have done several tutorials and didn't come across string substitution yet, so I'v been googling that part. I'm indeed using python3 so that's probably my first issue.

I noticed you mentioned my for loop overwriting the variable with each iteration but changed your reply so it's gone. That was a relevant remark as well though, I'm kinda ashamed I didn't notice that myself.

Sorry about that. When I first read your post, I thought this was for displaying all vehicles for all dealers. I figured you were iterating through a list of dealerNames, but only getting one dealer's worth of dealerVehicles. If that's the case, then yes, you'll want to append dealerVehicles to a list or dictionary at the end of the loop, like this:

Python code:
@app.route('/dealers/<dealerName>/')
def show_dealer(dealerName):
    dealerName = query_db('select distinct dealer from motorcycles')
    dealerVehicles = dict()
    for dealer in dealerName:
        vehicles = query_db("select * from motorcycles where dealer = %s" % (dealer))
        dealerVehicles[dealer] = vehicles
    return render_template('layout.html', dealerName=dealerName, dealerVehicles=dealerVehicles)
Then your layout.html might have:

HTML code:
{% for dealer, vehicles in dealerVehicles.items() %}
<div>
  <h1>{{ dealer }}</h1>
  <ul>
    {% for vehicle in vehicles %}
    <li>{{ vehicle }}</li>
    {% endfor %}
  </ul>
</div>
{% endfor %}

Cingulate
Oct 23, 2012

by Fluffdaddy
str.format is also in py2, what do you guys mean?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Just a heads up for good programming practices, don't do string interpolation on sql queries:

Python code:
dealerVehicles = query_db("select * from motorcycles where dealer = %s" % dealer)
or
Python code:
dealerVehicles = query_db("select * from motorcycles where dealer = {0}".format(dealer))
Use the database library interface instead:

Python code:
dealerVehicles = cursor.execute("select * from motorcycles where dealer = ?", [dealer])
Read up on sql injection and prepared statements to know more about why this is important for eventual security and performance concerns.

Tigren
Oct 3, 2003

Cingulate posted:

str.format is also in py2, what do you guys mean?

>= py2.6 to be exact I guess

Hed
Mar 31, 2004

Fun Shoe
I hate it in py2.6 because you have to provide indexes. But I hate py2 generally in 2016 :)

Cingulate
Oct 23, 2012

by Fluffdaddy

Hed posted:

I hate it in py2.6 because you have to provide indexes. But I hate py2 generally in 2016 :)
Supporting py2.6 is the worst.

VikingofRock
Aug 24, 2008




Is python 2.6 still the default python for CentOS? The only time I ever see it nowadays is on old CentOS machines.

LochNessMonster
Feb 3, 2005

I need about three fitty


Symbolic Butt posted:

Just a heads up for good programming practices, don't do string interpolation on sql queries:
Use the database library interface instead:

Python code:
dealerVehicles = cursor.execute("select * from motorcycles where dealer = ?", [dealer])
Read up on sql injection and prepared statements to know more about why this is important for eventual security and performance concerns.

my query_db function is based on the following functions, so I am using cursor.execute, just calling it with another function. For some reason I just can't get it to work.

Python code:
#db functions
def connect_db():
    rv = sqlite3.connect(app.config['DATABASE'])
    rv.row_factory = sqlite3.Row
    return rv

def get_db():
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db

def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

#url functions
@app.route('/dealers/<dealerName>/')
def show_dealer(dealerName):
    dealerName = query_db('select distinct dealer from motorcycles')
    dealerVehicles = dict()
    for dealer in dealerName:
        vehicles = query_db("select * from motorcycles where dealer = ?", [dealer])
        dealerVehicles[dealer] = vehicles
    return render_template('dealer.html', dealerName=dealerName, dealerVehicles=dealerVehicles)
Error message I get from Flask is:

code:
File "/path/to/project/scraper-frontend.py", line 36, in query_db
    if not hasattr(g, 'sqlite_db'):
        g.sqlite_db = connect_db()
    return g.sqlite_db
 
def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv
 
#Test functions
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
I've been trying to google the error message, but only found anwsers that say the query should be in the format you already suggested.


edit: maybe I should clearify what I'm trying to achieve a bit better. I currently have 40ish dealers. For each dealer I'd like to have a url ../dealers/<dealerName> which displays all vehicles for this dealer.

I still have a feeling I'm doing this completely wrong, but can't figure out what.

edit2: I managed to get it working with (dealer) instead of [dealer]

Python code:
vehicles = query_db("select * from motorcycles where dealer = ?", (dealer))
It now returns sqlite3.Row.object, so I just need to make sure to pick the columsn I'd like to display.

And it displays all vehicles/dealers on each page, but that's something I need to fix in the jinja template.

LochNessMonster fucked around with this message at 10:51 on Nov 3, 2016

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

That error message is saying the parameter you're feeding in (dealer) is the wrong type. It's probably expecting a string or another primitive like an int, and dealer is probably some kind of record object instead. Try passing in str(dealer)

e- me read good

baka kaba fucked around with this message at 14:48 on Nov 3, 2016

Space Kablooey
May 6, 2009


Minor nitpick on your query_db function.

When you want to return only one result, you shouldn't pick the first result from cur.fecthall, you should use cur.fetchone instead, like so:

Python code:
def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    if one:
        rv = cur.fetchone()
    else:
        rv = cur.fetchall()
    # or you can replace the if else with 
    # `rv = cur.fetchone() if one else cur.fetchall()` if you are feeling cheeky
    cur.close()
    return rv

Dominoes
Sep 20, 2007

For anyone interested, I'm posting part of my ipython config, which includes auto-imports of common math(s) functions and constants. Ie, like Julia:

Python code:
# lines of code to run at IPython startup.
c.InteractiveShellApp.exec_lines = [
    '%load_ext autoreload',
    '%autoreload 2',

    # Arrays, dataframes, and trig functions
    'import numpy as np',
    'from numpy import array, linspace, arange, zeros, ones, \
        eye, sin, cos, tan, arcsin, arccos, arctan, log, sqrt',
    'np.set_printoptions(suppress=True, precision=4)',
    'import pandas as pd',
    'from pandas import DataFrame, Series',

    # Dates and times
    'import saturn',

    # Functional programming
    'from functools import partial',
    'from cytoolz import *',

    # Plotting
    'import matplotlib',
    'from matplotlib import pyplot as plt',
    'import fplot',

    # Mathematical constants
    'import math',
    'from math import e, pi',
    'tau = 2 * pi',
    '&#960;, &#964; = pi, tau',   # SA's mangling the unicode. :/
    'i = complex(0, 1)',

    # Sympy
    'import sympy',
    'from sympy import diff, integrate, exp, oo, sin as ssin, cos as scos, \
        tan as stan, asin as sasin, acos as sacos, atan as satan, Matrix, simplify, \
        lambdify, Integral, Derivative, factor, expand, limit, var, Eq, N, \
        solveset, linsolve, roots, dsolve, symbols, log as slog, sqrt as ssqrt, \
        cbrt, pi as spi, Rational, linsolve',
    'from sympy.plotting import plot',
    "x, y, z, t = sympy.symbols('x y z t')",
    'sympy.init_printing()',
]

Cingulate
Oct 23, 2012

by Fluffdaddy
Which one should I do? (Or: when should I do which)

code:
# 1
numbers = (100, 200, 300)

for number in some_list:
    if number in numbers:
        print("hey")

# 2
for number in some_list:
    if number in (100, 200, 300):
        print("hey")

QuarkJets
Sep 8, 2008

Cingulate posted:

Which one should I do? (Or: when should I do which)

code:
# 1
numbers = (100, 200, 300)

for number in some_list:
    if number in numbers:
        print("hey")

# 2
for number in some_list:
    if number in (100, 200, 300):
        print("hey")

I think #1 is better for both readability and computational efficiency, but you should use a set instead of a tuple, aka set numbers to set((100,200,300))

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Cingulate posted:

Which one should I do? (Or: when should I do which)

code:
# 1
numbers = (100, 200, 300)

for number in some_list:
    if number in numbers:
        print("hey")

# 2
for number in some_list:
    if number in (100, 200, 300):
        print("hey")

Do #2 when the collection is only small, is used in only one place, and is not configurable.

code:
for char in s:
    if char in ("+", "-", "*", "/"):
        print("Found a maths character")
Otherwise do #1.

Eela6
May 25, 2007
Shredded Hen
Cingulate, the answer is 'it depends'. If there's some name that accurately describes your container's contents, use that. On the other hand, sometimes your container explains itself.

EG:
Python code:
# good - makes it clear why we chose these elements
uniformColors= {'black', 'blue', 'white')
icecreamFlavors = {'chocolate', 'vanilla', 'strawberry'}

# debatable - probably useful. saves a little bit of brain space when reading over the code.
powersOfFive = {5**n for n in range(10)}

# silly
yesOrNo = {'yes', 'no'}
abcde = set('abcde')
It's generally a matter of best practice to do membership testing with sets. As mentioned above, you can create sets with the set() constructor, or directly with curly braces.

If your possible elements are very small, it's OK to test membership in a list or tuple. But it's good to get in the habit of testing membership in sets, because this will save you headaches down the line.

Testing membership in a list or tuple is O(n), where testing membership in a set via hash function is O(1).

Python code:
# equivalent:
membership = {'a', 'b', 'c'}
membership = set(('a', 'b', 'c')) 

Eela6 fucked around with this message at 03:25 on Nov 10, 2016

Cingulate
Oct 23, 2012

by Fluffdaddy
Thanks guys!

Eela6 posted:

Testing membership in a list or tuple is O(n), where testing membership in a set via hash function is O(1).
Is there a rule for set vs. tuple creation?

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
They're both O(n), though the CPU time per element is higher for sets than lists or tuples since each element has to be hashed instead of simply storing a reference in the collection. If you're only doing a single membership test (i.e. not in a loop), you may as well just check for item in list or item in tuple -- you don't really recoup the cost of building a set until at least the second membership test.

Cingulate
Oct 23, 2012

by Fluffdaddy
Intuitively, I'd use the tuple instead of the list then because for a one-shot, it seems more appropriate. (Or the set if it's longer or reused or whatever)

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
Huge caveat, though: what I said only applies if you're storing the collection in a variable and then performing membership tests after that (or if the collection can change at runtime, of course). When using a set literal, with values known at compile-time, Python's peephole optimizer replaces the set with a frozenset which is precomputed when the code is compiled:

code:
>>> def check(item):
...     return item in {'a', 'b', 'c'}
... 
>>> from dis import dis
>>> dis(check)
  2           0 LOAD_FAST                0 (item)
              3 LOAD_CONST               4 (frozenset({'c', 'b', 'a'}))
              6 COMPARE_OP               6 (in)
              9 RETURN_VALUE

QuarkJets
Sep 8, 2008

Cingulate posted:

Intuitively, I'd use the tuple instead of the list then because for a one-shot, it seems more appropriate. (Or the set if it's longer or reused or whatever)

You'd want to use the set if you're checking for membership more than once, because otherwise membership checking is expensive (O(n)). And you don't want to define the set inside of the for loop because defining a set is expensive (O(n)).

Thermopyle
Jul 1, 2003

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

The chances are super high that you should use whatever you want because the performance differences are going to be minuscule for your application.

Eela6
May 25, 2007
Shredded Hen

Thermopyle posted:

The chances are super high that you should use whatever you want because the performance differences are going to be minuscule for your application.

Yes. I tend to emphasize 'always test membership in sets! ' because most of the python programmers I know are academics who are not professional programmers. Teaching them this way avoids the worst - case scenarios.

mr_package
Jun 13, 2000
What are some good ways to learn how to structure Python programs? I've been looking at some of the projects listed here https://www.reddit.com/r/Python/comments/1ls7vq/best_written_projects_on_python_github/ but not seeing the decision process that goes behind the design. Part of the issue is that my scripts are very linear so I don't really need to make much of OO design. But I worry I'm missing other things due to not having a CS background. Suggestions? I've been writing scripts for literally years but when I look at a big project like Tornado or Flask it seems very different than something I would write. I've read all the books but they're all about syntax and not really about the big picture/design.

mr_package fucked around with this message at 00:33 on Nov 11, 2016

Dominoes
Sep 20, 2007

-

Thermopyle
Jul 1, 2003

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

mr_package posted:

What are some good ways to learn how to structure Python programs? I've been looking at some of the projects listed here https://www.reddit.com/r/Python/comments/1ls7vq/best_written_projects_on_python_github/ but not seeing the decision process that goes behind the design. Part of the issue is that my scripts are very linear so I don't really need to make much of OO design. But I worry I'm missing other things due to not having a CS background. Suggestions? I've been writing scripts for literally years but when I look at a big project like Tornado or Flask it seems very different than something I would write. I've read all the books but they're all about syntax and not really about the big picture/design.

Unfortunately, I don't know a good answer for this other than writing a lot of code.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

mr_package posted:

What are some good ways to learn how to structure Python programs? I've been looking at some of the projects listed here https://www.reddit.com/r/Python/comments/1ls7vq/best_written_projects_on_python_github/ but not seeing the decision process that goes behind the design. Part of the issue is that my scripts are very linear so I don't really need to make much of OO design. But I worry I'm missing other things due to not having a CS background. Suggestions? I've been writing scripts for literally years but when I look at a big project like Tornado or Flask it seems very different than something I would write. I've read all the books but they're all about syntax and not really about the big picture/design.
It's definitely going to depend on what you're doing.

I recommend reading through at least the Python parts of this book, though. Much of the book is in Python, and about Python projects, so just pick some chapters that seem interesting and dive in.

EDIT: Also, look at the code of the projects you use a lot. I use Requests all the drat time, and I've browsed the code when i have questions about the internals and learned a bunch. Since you probably understand the API and how to use it, you can get a good grasp of the internals pretty quickly.

Also, the book has a chapter by Guido on asyncio, which loving rules

Ghost of Reagan Past fucked around with this message at 18:14 on Nov 12, 2016

mr_package
Jun 13, 2000
Thanks, I don't think these books ever came up in my research on this I will give it a read.

I was in fact just thinking it made little sense to try to study Flask when I'm not writing anything web-app. Too bad pyodbc is C. But I did work with Pillow/PIL a lot in the past so maybe that's a place to start. I wrote some functions that might even be good to contribute back to that project actually.

Womens Jeans
Sep 13, 2007

by LITERALLY AN ADMIN
What is a good python IDE that integrates will with Docker?

I write 99% of my code in R, and I have RStudio Server loaded into a docker container (with all appropriate libraries installed via the Docker file). I then load my R scripts into the container by sharing volumes/folders and work on them in the IDE interactively via the browser (ie RStudio Server). I can then run my finished code using the R command line executable from within the Docker container.

Is there anything like that for Python? Basically I want to be able to write code in an interactive way, and then be able to run it via command line without changing anything. The command line part needs to be able to be completely encapsulated withon Docker for portability reasons.

Thermopyle
Jul 1, 2003

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

Womens Jeans posted:

What is a good python IDE that integrates will with Docker?

I write 99% of my code in R, and I have RStudio Server loaded into a docker container (with all appropriate libraries installed via the Docker file). I then load my R scripts into the container by sharing volumes/folders and work on them in the IDE interactively via the browser (ie RStudio Server). I can then run my finished code using the R command line executable from within the Docker container.

Is there anything like that for Python? Basically I want to be able to write code in an interactive way, and then be able to run it via command line without changing anything. The command line part needs to be able to be completely encapsulated withon Docker for portability reasons.

PyCharm. (I don't know if you can exactly mirror your R experience, but I think it does have Docker support, and I do know it has support for working with remote interpreters)

Thermopyle fucked around with this message at 16:43 on Nov 13, 2016

BigRedDot
Mar 6, 2008

Womens Jeans posted:

What is a good python IDE that integrates will with Docker?

I write 99% of my code in R, and I have RStudio Server loaded into a docker container (with all appropriate libraries installed via the Docker file). I then load my R scripts into the container by sharing volumes/folders and work on them in the IDE interactively via the browser (ie RStudio Server). I can then run my finished code using the R command line executable from within the Docker container.

Is there anything like that for Python? Basically I want to be able to write code in an interactive way, and then be able to run it via command line without changing anything. The command line part needs to be able to be completely encapsulated withon Docker for portability reasons.

The new JupyterLab under development has lots of plugins and various different modes in addition to the "Classic Jupyter Notebook" interface, some of them are more like IDEs, ala RStudio

Zekky
Feb 27, 2013
Got a few months before I start a new job and I want to use it to brush up on my Python skills, especially data analysis. Can anyone recommend any good books or online courses ideally focusing on the SciPy/NumPy stack? I've done a few online courses so I'm familiar with all the components but I'd like something a bit more in-depth and advanced than what's available on EdX. Thanks!

Cingulate
Oct 23, 2012

by Fluffdaddy

Zekky posted:

Got a few months before I start a new job and I want to use it to brush up on my Python skills, especially data analysis. Can anyone recommend any good books or online courses ideally focusing on the SciPy/NumPy stack? I've done a few online courses so I'm familiar with all the components but I'd like something a bit more in-depth and advanced than what's available on EdX. Thanks!
Participate in a Kaggle competition?

Zekky
Feb 27, 2013

Cingulate posted:

Participate in a Kaggle competition?

Looks cool thanks for the tip. Surprised I've never seen this before actually.

KernelSlanders
May 27, 2013

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

Zekky posted:

Got a few months before I start a new job and I want to use it to brush up on my Python skills, especially data analysis. Can anyone recommend any good books or online courses ideally focusing on the SciPy/NumPy stack? I've done a few online courses so I'm familiar with all the components but I'd like something a bit more in-depth and advanced than what's available on EdX. Thanks!

Do your own project and put it on your github for next time you're looking for a job. Write a library, do some analysis of something.

axolotl farmer
May 17, 2007

Now I'm going to sing the Perry Mason theme

I'm using pandas to update a column in a dataframe.

My rules are if there is a value in the New column, that becomes the Current value.

If there is a NaN in the New column, the value in the Old column becomes the Current value

code:
In[]: df = pd.DataFrame([[1, 2,np.nan],[3, 2,np.nan],[7, np.nan,np.nan], [np.nan, 8,np.nan]], columns=['Old', 'New', 'Current'])
In[]: df
Out[]: 

   Old  New  Current
0  1.0  2.0      NaN
1  3.0  2.0      NaN
2  7.0  NaN      NaN
3  NaN  8.0      NaN
I try to put in the values from New and then replace the NaN from Old.

code:
In[]df.Current=df.New
In[]df.Current=df.Current.loc[(df.Current.isnull() & (df.Old.notnull()))] = df.Old
In[]df
Out[]: 

   Old  New  Current
0  1.0  2.0      1.0
1  3.0  2.0      3.0
2  7.0  NaN      7.0
3  NaN  8.0      NaN
Welp, this just replaces all the values in Current with Old.

Please help, I'm bad and new at this.

Eela6
May 25, 2007
Shredded Hen
You want to use numpy / MATLAB style logical indexing.

Remember not to use the bitwise operators like '&' unlesss you are actually working bitwise. Apparently this is a difference between numpy and pandas

numpy has a number of formal logic operators that are what you want, called logical_and, logical_not, logical_xor, etc...

It's easiest to understand given an example. You might already know this, but it's always nice to have a refresher.

IN:
Python code:
A = np.array([2, 5, 8, 12, 20])
print(A)
between_twenty_and_three = np.logical_and(A>3, A<20)

print(between_twenty_and_three)
A[between_twenty_and_three] = 500

print(A)
OUT:
Python code:
[ 2  5  8 12 20]
[False  True  True  True False]
[  2 500 500 500  20]
Specifically, for your question:
IN:
Python code:
def update_dataframe(df: pd.DataFrame) -> pd.DataFrame:
    df.Current = df.New
    df.Current[pd.isnull(df.New)] = df.Old[pd.isnull(df.New)]
    return df
    
def test_update_dataframe():
    df = pd.DataFrame([[1, 2,np.nan],
                   [3, 2,np.nan],
                   [7, np.nan,np.nan], 
                   [np.nan, 8,np.nan]],
                  columns=['Old', 'New', 'Current'])
    print('old')
    print(df)
    df = update_dataframe(df)
    print('new')
    print(df)    
    
test_update_dataframe()
OUT:
code:
old
   Old  New  Current
0  1.0  2.0      NaN
1  3.0  2.0      NaN
2  7.0  NaN      NaN
3  NaN  8.0      NaN
new
   Old  New  Current
0  1.0  2.0      2.0
1  3.0  2.0      2.0
2  7.0  NaN      7.0
3  NaN  8.0      8.0

Eela6 fucked around with this message at 23:11 on Nov 18, 2016

vikingstrike
Sep 23, 2007

whats happening, captain
I'll have to look closer when not on a phone but can definitely use & and | for row indexing in pandas. I'm pretty sure your .loc[] commands are slightly wrong from a quick glance.

Here's what you want
code:
df.loc[:, 'Current'] = df.loc[:, 'New']
df.loc[(df.Current.isnull())&(df.Old.notnull()), 'Current'] = df.loc[(df.Current.isnull())&(df.Old.notnull()), 'Old']

vikingstrike fucked around with this message at 20:57 on Nov 18, 2016

Adbot
ADBOT LOVES YOU

Cingulate
Oct 23, 2012

by Fluffdaddy
Not Pandas style, but what about

code:
df["current"] = [old if np.isnan(new) else new for new, old in zip(df["New"], df["Old"])]

  • Locked thread