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
Dominoes
Sep 20, 2007

I'm new to Python and programming. Which GUI do you recommend? There's lots of info on the internet, with conflicting opinions, much of which may be out of date. The most popular seem to be TKinter and wxPython. I've heard each is easier to use from different sources, and wxPython looks better/more native for every OS. I've also heard reverences to gtk and qt. Which do you prefer, and why?

Any tips for getting notepad++ to run Python code through IDLE?

Dominoes fucked around with this message at 15:52 on Mar 20, 2013

Adbot
ADBOT LOVES YOU

Dominoes
Sep 20, 2007

Thanks for the GUI advice dudes - I'm going to use QT.

aeverous posted:

I dunno why you want it through IDLE but try using the PyNPP plugin for Notepad++ and bind a key to run scripts.
IDLE is the only program I know of that runs uncompiled python scripts, other than a command line. What do you recommend instead? I'll try PyNPP.

Is there a trick to getting the pyPdf module to work? I've been able to get several other modules to install and work, including Pip, Py2exe, requests and Rauth.

If I run 'pip install pypdf', it appears to install, and populates pypdf and pypdf.... egg-info folders in python27/lib/site-packages. However, I get a crash at import module, both with 'import pypdf' and import 'pypdf2'. If I try to 'pip install pypdf2', pip can't find the package. I downloaded pypdf manually from here. Running the setup script from command prompt (setup build / setup intstall), I get an error about not finding the pypdf2 directory to install things with. If I edit the script and change the directory to the precise path, it installs a PyPDF2-1.15-py2.7.egg-info file in site-packages, but no folders, and the import command still bugs out.

Dominoes fucked around with this message at 22:32 on Mar 21, 2013

Dominoes
Sep 20, 2007

accipter posted:

I think you might want to try to install pypdf2, since pypdf stopped being developed 3 years ago.
I think that's what I'm trying to install.

Dominoes
Sep 20, 2007

Can anyone help me with a Python 2 to 3 conversion problem? I'm switching to Python 3, and one of the functions in a project I'm working on won't convert well. I'm looping through a website's possible urls for files until I find a valid one with URLLIB. I'm using urlopen, and verifying that the file size is a reasonable size. However, now in Python 3, I get a crash with HTTP errors when I present an invalid URL, instead of passing a bogus file, realizing it's bogus and continuing the loop.

Python 2 working code:
code:
from urllib import urlopen

def find_version():
    #Determines latest FAA procedure version by checking downloaded file size for descending dates
    print "Determining latest procedure version..."
    i = ((date.today().year + 1) % 100) * 100 + 12    
    while i > (date.today().year % 100) * 100:
        url = 'http://aeronav.faa.gov/d-tpp/%s/%s.PDF' % (str(i), all_plates[0].file_name)
        web_file = urlopen(url)
        if int(web_file.info()['Content-Length']) > 10000:
            return str(i)
        i -= 1
        if i % 100 == 0:
            i -= 88
Python 3 broken code.
code:
from urllib.request import urlopen

def find_version():
    #Determines latest FAA procedure version by checking downloaded file size for descending dates
    print ("Determining latest procedure version...")
    i = ((date.today().year + 1) % 100) * 100 + 12    
    while i > (date.today().year % 100) * 100:
        url = ('http://aeronav.faa.gov/d-tpp/%s/%s.PDF' % (i, all_plates[0].file_name))
        web_file = urlopen(url)
        if int(web_file.info()['Content-Length']) > 10000:
            return str(i)
        i -= 1
        if i % 100 == 0:
            i -= 88
Thhe code 'web_file = urlopen(url)' crashes the program if presented with an invalid URL. Passing invalid URLs to it is the point of this function, and it worked as intended before.

Error:


I've spent lots of time Googling this, and haven't found a solution.

Dominoes
Sep 20, 2007

accipter posted:

Why don't you just surround the urlopen in a try/catch statement on urllib.error.HTTPError?
Thank you - I got a working solution using this. More elegant than checking the file size. At first I was trying to enter the type of exception after 'except', but couldn't figure it out, so I tried leaving that parameter blank, and it worked. Here's an excerpt from the working code:

code:
try:
            urlopen(url)
            print ("Version: " + str(i))
            return i
        except:
            i -= 1
            if i % 100 == 0:
                i -= 88

Houston Rockets posted:

Why not use requests or urllib2?

Also the docs state that:


So if you were on <= Python 2.6, that could account for the difference.
I was using Python 2.7. My understanding from reading some Python docs is that 'urllib' in Python 3 is urllib 2 from python 2. I ended up downloading something called urllib 3 in the process; not sure what it does. The syntax in Python 3 is a bit different, as reflected in the two code snippets I posted; Python 3 uses 'urllib.request.urlopen' instead of 'urllib.urlopen'.


BeefofAges posted:

Just stop what you're doing and use requests.
I'll look into that as an alternate solution.

It's interesting how I don't need to (am no longer allowed to) convert the iterator 'i' to a str in Python 3, when it was required in Python 2.

Dominoes fucked around with this message at 05:07 on Mar 23, 2013

Dominoes
Sep 20, 2007

BeefofAges posted:

Don't do this. This will catch every exception.

You want 'except urllib.error.HTTPError as e:'
Thanks. Was trying to figure out how to do this, but it wouldn't take the error type. I think I was missing the 'as e'. It works now, with
code:
from urllib.error import HTTPError
and
code:
except HTTPError as e:

Dominoes
Sep 20, 2007

Learning Qt with no programming experience is proving to be a PITA. The Zetcode tutorial has some great example code that I've been able to use and manipulate, but is fairly limited in what it demonstrates.

There are several other tutorials online, all of which seem to use very different syntax - and I can't get any of them to work. I setup Qt Designer, and have been creating windows, then analyzing the code using puic, but can't get the gui to display, even when trying wrapper styles from several tutorials. Can anyone explain how to get QT Designer 5 code ->puic to run in Python 3? I'm not getting any errors, just an output of >>>.

What style do you recommend as the best way to learn Qt? I'm planning to stick with the Zetcode style, but am having some issues doing things not directly described in it: For example, how can I have two separate layout widgets in a main window? This seems dramatically more difficult than learning basic Python - it might be due to lack of strong tutorials, like Codeacademy's.

Dominoes fucked around with this message at 04:29 on Mar 29, 2013

Dominoes
Sep 20, 2007

What's the most current form of Oauth for Python 3? I'm trying to create a stock screener using my broker's API, which uses Oauth. Most of the info I find is out of date or conflicting. I've seen the following modules referenced:

Oauth - seems to be the original, now outdated. I get an error of "'module' object has no attribute 'Consumer'"
Oauth2 - Newer version, apparently also outdated? The one most referenced one online. Glitches out in pip/can't figure out how to install it.
Oauthlib - IIRC, claims to be the new replacement for Oauth and Oauth2
Rauth.OAuth2Service - Also potentially replacement for Oauth and Oauth2?
Requests - ?
Oauth_hook - ?
pyoauth2 - I get an error about not having a module named "client" in pyoauth2's init.

Which should I use?

Dominoes fucked around with this message at 18:39 on Mar 31, 2013

Dominoes
Sep 20, 2007

I'm new to programming (Finished Codeacademy labs recently), and am running into long data-crunching times on a stock screener I'm working on. The functions below parse a .CSV file and compare its values to input numbers. Doing this for a maximum* of 20,000 cells takes 1 minute. *The actual number should be smaller, since a failed cell should skip the rest of its row. (The break in the second function below)

Is the time due to sloppy code? Is it normal? Is it a limit of python, compared to C++? Python's using 35% of my CPU while the evaluation's occurring.

The 'evaluate' function is a method of a class whose instances are contained in 'parameters' input for the 'passing_stocks' function. I removed some dubugging, output and formatting code.
code:
def evaluate(self, x, y):
#Determines if a data.csv cell is between the Parameters instance's floor and ceiling
    cell_value = read_cell(x, y, 'data.csv')
    if self._floor <= float(cell_value) <= self._ceil:
        return True
    else:
        return False
code:
def passing_stocks(stocks, parameters):
#Runs the evaluate function for the input stock and parameter lists
    result = []
    y = 0
    for n2 in stocks:
        failed = False
        for n in parameters:
            if n.evaluate(n.csv_col, y) == False:
                failed = True
                break
        if failed == False:
            result.append(n2)
        y += 1
    return result

Dominoes fucked around with this message at 16:32 on Apr 3, 2013

Dominoes
Sep 20, 2007

a lovely poster posted:

Is there any way you can the data you're pulling to do some of the heavy lifting for yourself? What kind of screens do you want to use? How many stocks are you pulling information for?

Python is not really great for real-time financial stuff because of it's slower speed. Honestly anything dealing with real-time market data is not a good project for a beginner programmer. Good luck!

I want to screen for price, % change, average volume, and possibly some other real-time criteria. I also want to screen for price changes over selectable increments of time - a function I haven't found on any existing screener.

I'm using Yahoo Finance. For now, I'm using a canned list of stocks pulled from the nyse, nasdaq and s&p500 website - about 5,000 total. I'm pulling the real-time data into a .csv, in 200-stock-per-query increments, the max allowed by Yahoo. Surprisingly, this takes substantially less time than the actual number crunching.

I'm also pulling and analyzing historical price data, which the functions I posted don't deal with. I'll post them, or the whole program if you're curious. Unfortunately, I can only seem to pull the historical data one-stock-at a time, so I've been pulling and analyzing a stock, then looping, instead of pre-downloading. I'm only pulling/analyzing historical data for stocks that passed the real-time screening.

I'm trying to set up the program to work with my broker (tradeking) as well, but I'm having trouble getting Oauth to work with Python 3 - that's what my previous post in this thread was about.

Dominoes fucked around with this message at 16:47 on Apr 3, 2013

Dominoes
Sep 20, 2007

DARPA posted:

What does the function read_cell look like?
Here's the read_cell function:
code:
from csv import reader

def read_cell(x, y, file_name):
#Returns a cell value in a csv
    with open(file_name, 'r') as f:
        myreader = reader(f)
        y_count = 0
        for n in myreader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1
Perhaps there's a way to go directly to the row I want without looping through each one?

Dominoes fucked around with this message at 16:57 on Apr 3, 2013

Dominoes
Sep 20, 2007

Thanks guys - Really appreciate it. Going to fix it this evening!

Dominoes
Sep 20, 2007

Problem solved! I used evilentity's load_csv function. I added this line at the top of passing_stocks: "data = load_csv('data.csv')" Read cell now looks like this:

code:
def read_cell(x, y, data):
#Returns a cell value in a compound list
        y_count = 0
        for n in data:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1
I added a third parameter to evaulate called data, and added it to the call in passing_stocks, which now reads "if n2.evaluate(n2.csv_col, y, data) == False:"

Pivotal Lever posted:

Another tip, if there's a module for what you're trying to do in the standard library, use it instead of rolling your own solution.
Are you referring to the list() function, or is there something else I can replace?

Dominoes
Sep 20, 2007

DARPA posted:

You are still walking the full length of the lists. You can directly address the cell.

You can write :
Python code:
cell = data[y][x]
and get rid of your for loop. You don't even need to call read_cell() at all now. You can just access the cell directly inside evaluate() using data[y][x].
Nice; that worked. Removing the repeated disk reads reduced the time from a minute to a second. Getting rid of the line-by-line read loop made the load instantaneous.

Dominoes fucked around with this message at 22:23 on Apr 3, 2013

Dominoes
Sep 20, 2007

Another CSV / memory vs drive write question. If I want to download a CSV from a url, is there a way I can put the file information directly into a variable in memory? I've tried many combinations using list(), read(), csv.reader() etc, but can't find a working solution that skips writing the csv to disk, and reading it.

Code like this works:
Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        with open('drive_file.csv', 'wb') as f:
            f.write(u.read())
        
        with open('drive_file.csv', 'r') as f:
            return list(csv.reader(f))
For example, this seems like it should do what I'm asking, but doesn't.

Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        return list(csv.reader(u.read()))

Dominoes
Sep 20, 2007

maniacripper posted:

Should I bother with ver2 at all? Should I just start learning ver3?

I'm learning Python, and chose Python 3 because it's the newer, refined version. You're more likely to run into compatibility problems with external modules in Python 3, since most of the information and development resources available are for Python 2. Philosophically, I suggest using Python 3 - you'd be helping push the community to upgrade.

Dominoes fucked around with this message at 19:48 on Apr 7, 2013

Dominoes
Sep 20, 2007

Is there a way to procedurally change a function based on variables, like in a loop? The best way I can describe it is with psuedocode:

Python code:
for n in range(:10):
            self.ui.checkBox_%s.stateChanged.connect(self.check_%s) % (n, n)
In this example, I'd like to create signals for 10 QT checkboxes, without writing each one out.

Dominoes
Sep 20, 2007

Hammerite posted:

There's probably a better way to do this, and I'm tired or I would think about it and suggest one. But yes you can do that using __getattribute__.

Python code:
for n in range(:10):
    ( self.ui
          .__getattribute__('checkBox_%s' % n)
          .stateChanged
          .connect(self.__getattribute__('check_%s' % n))
          )
Totally untested, so there is probably a mistake in there that stops it working.
No - your code works perfectly. Thank you! I haven't seen that syntax before, but it's evidently what I'm looking for.

Dominoes
Sep 20, 2007

Is there a working PDF manipulation module for Python 3? I've tried Pypdf, but it glitches out when I try to install with PIP. I'd like to merge PDF files. If I use Pypdf2, I get the following message using this code:

code:
with open('test1.pdf', 'rb') as f:
    with open('test2.pdf', 'rb') as f2:
        merger = PdfFileMerger()
        merger.merge(position=0, fileobj=f2)
        merger.merge(position=0, fileobj=f)
        merger.write(open("test_out.pdf", 'wb'))


"File "c:\...merger.py", line 97, in merge
elif type(fileobj) == file:
NameError: global name 'file' is not defined"

Line 97 of merger.py is " elif type(fileobj) == file:"

I get similar errors in my own code when using code such as

"input1 = PdfFileReader(file("document1.pdf", "rb"))" - that's a copy and paste from http://www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/

Dominoes fucked around with this message at 02:22 on Apr 11, 2013

Dominoes
Sep 20, 2007

I'm hoping someone can help me with a QT / designer question. The example code I've included shows how I've been setting up the code, which may not be correct. I'm trying to get programs that are not in classes that work with QT directly to change aspects of the GUI. I've set up example code showing the structure of my programs. My goal is to get the function btn_about2, in the main program (or another class) to change the main window's statusbar. I've included two windows in this example, to clarify how my gui code's structured.

Python code:
from PyQt4 import QtCore, QtGui
from main_gui import Ui_Main
from about_gui import Ui_About
#main_gui and about_gui are .py files generated by designer and pyuic

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Main()
        self.ui.setupUi(self)
	#I don't really know what this code is, but I need it to make the program work
        
        self.ui.actionAbout.triggered.connect(self.btn_about) #or about2
  	#Signals go here, and call this class's methods, which call other methods.
        #I can't seem to call other methods/functions directly, and these won't take arguments.

    def btn_about(self):
    #Referenced by the above code. Can interact with other classes/functions.
        self.ui.statusbar.showMessage("This works!")
        self.w = About()
        self.w.show()


class About(QtGui.QWidget):
#Separate windows/popups  etc have their own class
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_About()
        self.ui.setupUi(self)


def btn_about2(self):
   StartQT4.ui.statusbar.showMessage("This doesn't work!")
    #I've tried many variations of the above line, with no luck.
    w = About()
    w.show() #This however works; the popup appears.

#More classes and functions not directly-related to the GUI go here; ie the most of the program.

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
Related - Outside of learning QT, I've been using classes for two purposes: Organizing similar functions, and setting up variables with multiple sub-variables. (objects with attributes) The way QT/designer uses them doesn't seem to fit with this understanding of classes, which may be the problem.

Dominoes fucked around with this message at 15:12 on Apr 13, 2013

Dominoes
Sep 20, 2007

Dominoes posted:

I'm hoping someone can help me with a QT / designer question. The example code I've included shows how I've been setting up the code, which may not be correct. I'm trying to get programs that are not in classes that work with QT directly to change aspects of the GUI. I've set up example code showing the structure of my programs. My goal is to get the function btn_about2, in the main program (or another class) to change the main window's statusbar. I've included two windows in this example, to clarify how my gui code's structured.

Python code:
from PyQt4 import QtCore, QtGui
from main_gui import Ui_Main
from about_gui import Ui_About
#main_gui and about_gui are .py files generated by designer and pyuic

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Main()
        self.ui.setupUi(self)
	#I don't really know what this code is, but I need it to make the program work
        
        self.ui.actionAbout.triggered.connect(self.btn_about) #or about2
  	#Signals go here, and call this class's methods, which call other methods.
        #I can't seem to call other methods/functions directly, and these won't take arguments.

    def btn_about(self):
    #Referenced by the above code. Can interact with other classes/functions.
        self.ui.statusbar.showMessage("This works!")
        self.w = About()
        self.w.show()


class About(QtGui.QWidget):
#Separate windows/popups  etc have their own class
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_About()
        self.ui.setupUi(self)


def btn_about2(self):
   StartQT4.ui.statusbar.showMessage("This doesn't work!")
    #I've tried many variations of the above line, with no luck.
    w = About()
    w.show() #This however works; the popup appears.

#More classes and functions not directly-related to the GUI go here; ie the most of the program.

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())
Related - Outside of learning QT, I've been using classes for two purposes: Organizing similar functions, and setting up variables with multiple sub-variables. (objects with attributes) The way QT/designer uses them doesn't seem to fit with this understanding of classes, which may be the problem.
Got the solution: Needed to use the instance of the window class, not the class itself. The working btn_2 code is
code:
myapp.ui.statusbar.showMessage("Hooray it works!")
Another question: What's the best way to reference a relative path that's not the directory of the .py file? For example, I'd like to have a subdirectory where I save files. I had been using str(os.path[0]), but find that when I run a compiled version (Windows), it glitches out and references a directory in the exe. Ie: if the directory's "C:\programs\myprogram\program.py", and I use str(os.path[0]) + '\\path', it tries to go to "C:\programs\myprogram\program.exe\path." I can work around this by setting os.path[1] instead of 0, but then the uncompiled script doesn't work.

Method 2: using os.path.dirname(__file__). This works in the uncompiled script, but the compiled program loads the GUI, then glitches out with the following error:

Dominoes fucked around with this message at 00:38 on Apr 15, 2013

Dominoes
Sep 20, 2007

I finished my first program! I'm having trouble making a binary using Python 3 and cx_freeze. The binary runs on my computer, but on another computer I'm testing it on, I get the message "Can't find MSCVR100.dll". I looked this up, and it appears to be a visual studio C++ file.

I located the DLLin my Windows/sysWOW64 directory, and moved it into my application directory on the computer with the error. (Both are Win 8 64-bit) I now receive an error 0xc000007b, which appears to be a 64-bit compatibility error. This website describes receieving these two errors in sequence, presumably using C++. He found the fix to be installing Visual Studio. Doesn't make sense.

Very little of the information available on either the DLL or the 07b error mentions python or cx_freeze, which is why I'm stumped. The program does use QT, which I feel may have something to do with it. Any ideas?

Dominoes fucked around with this message at 04:27 on Apr 19, 2013

Dominoes
Sep 20, 2007

Plorkyeran posted:

Install the Visual Studio 2010 redistributable package.
I'm making a binary because I'd like to distribute this program to coworkers. I'd prefer the program to just work. There's got to be something I can add to my setup.py, or a way to get the proper .dll manually into the program's folder.

Dominoes
Sep 20, 2007

QuarkJets posted:

Do you have the Visual Studio 2010 redistributable package on the computer that you're using to create the binary?
No, but I have various 'Microsoft Visual C++ (year), x86 and x64 Redistrbutables' under the uninstall-a-program dialog.

Dominoes
Sep 20, 2007

The computer where the program's not working has an x86 visual c++ runtime, but not x64. It has mscrv100.dll in the syswow64 folder, but not the sys32. If I remove the .dll I added to the program folder, I get the missing .dll error. If I move it to sys32, I get the 000007b error.

This is a pretty simple program; there's got to be a better way. Whenever I'm thinking about installing a new program, and it makes me install some weird package, I get annoyed and consider not bothering. I don't want to program that way. I'm surprised more about this doesn't come up in searches about qt, cx_freeze etc; it seems like something lots of people would run into.

Does wx_widgets have this problem?

Dominoes fucked around with this message at 13:36 on Apr 19, 2013

Dominoes
Sep 20, 2007

Modern Pragmatist posted:

Any standalone executable you create for windows relies upon the c++ runtime regardless of which libraries you use. The runtime is required for the python interpreter that is included with the executable.

Have you read the page on this in the documentation?

Also you need to make sure that if you create a 64-bit executable then you need to have the 64-bit redistributable.

accipter posted:

It seems like underscores break URL tags...
code:
[url]http://cx_freeze.readthedocs.org/en/latest/overview.html#microsoft-visual-c-2008-redistributable-package[/url]
Thanks all! That page's explanation's what I was looking for. Installing C++ runtime 2010 x64 on the computer where the program was broken allows it to run. The page you linked has the file names for the 2008 version. Once I find the 2010 equivalents, this should work.

edit: Solved! The required files are msvcr100.dll and msvcp100.dll from the Sys32 directory.

Dominoes fucked around with this message at 17:02 on Apr 20, 2013

Dominoes
Sep 20, 2007

Lesson in reinventing the wheel today: I created a function that allows date addition and subtraction using modulus and floor division. Turns out the datetime function already does that! It was a good learning experience.

Dominoes fucked around with this message at 02:37 on Apr 21, 2013

Dominoes
Sep 20, 2007

Suspicious Dish posted:

And chances are you got it wrong.
There is some error, but it was acceptable for what I used it for. No more than 2 days of error for the first few years.

Dominoes
Sep 20, 2007

Technique question: I have two similar very similar, and lengthy bits of code (Pulling financial data from two different sources, one uses json, the other csv). There are common replaceable items. For example, where one function uses 'data[i][4]', the other might use 'data[n][day]['date']'. I'd pass '[n][day]' and '[i][4]' as parameters to the new function, reusing the code instead of having it exist twice. Logically, it seems easy to combine these into a common function, but simply turning them into arguments doesn't seem to work.

Option 1: Keep the functions separate due to this issue. Don't try to reuse the code

Option 2: I've read about using exec(), but it seems like a messy hack, and I can't get it working.

Option 3: A Python solution designed for this scenario that I haven't found. Someone earlier helped me with a similar problem using __getattribute__ with %s, but I can't seem to get it working in this scenario, and it also feels like a weird hack.

What's the preferred way to handle this?

example:
Python code:
data = ['a', 'b', 'c', 'd']
input1 = '[1]'
input2 = '[2]'

def func(data, input):
    return ''.join(data, input) #pseudocode
    
func(data, input1)
func(data, input2)

Dominoes fucked around with this message at 18:55 on Apr 21, 2013

Dominoes
Sep 20, 2007

Nippashish posted:

Python code:
data = ['a', 'b', 'c', 'd']
input1 = 1
input2 = 2

def func(data, input):
    return data[input]
    
func(data, input1)
func(data, input2)
What about this?
Python code:
data1 = ['a', 'b', 'c', 'd']
data2 = [['a', 'b', 'c', 'd'],['w', 'x', 'y', 'z']]
input1 = '[1]'
input2 = '[0][1]'

def func(data, input):
    return ''.join(data, input) #pseudocode
    
func(data1, input1)
func(data2, input2)
edit: Actually it looks like your solution works. Just need to pass the arguments as lists/tuples and account for all possible slots.

Python code:
data1 = ['a', 'b', 'c', 'd']
data2 = [['a', 'b', 'c', 'd'],['w', 'x', 'y', 'z']]
input1 = (1, 0)
input2 = (1, 2)

def func(data, input):
    return data[input[0]][input[1]]
    
func(data1, input1)
func(data2, input2)

Dominoes fucked around with this message at 19:42 on Apr 21, 2013

Dominoes
Sep 20, 2007

Thanks Plork - that works too.

Last one! I think this can be solved with an if statement asking which input format you're entering (as an additional parameter) and having two separate return lines, but I'm wondering if there's a cleaner way.

Python code:
data1 = ['a', 'b', 'c', 'd']
data2 = [['a', 'b', 'c', 'd'],['w', 'x', 'y', 'z']]
input1 = '[1]'
input2 = '[0][n]'

def func(data, input):
    for n in range(3):
        return ''.join(data, input) #pseudocode
    
func(data1, input1)
func(data2, input2)

Dominoes fucked around with this message at 20:09 on Apr 21, 2013

Dominoes
Sep 20, 2007

Suspicious Dish posted:

Please share more about your actual problem. This seems like overengineering at the highest level.
It's similar to the example I posted, but more complicated. I'm looking at historical stock data from Yahoo and Tradeking. I'm working with the Yahoo data as a .csv, and Tradeking as a .json phrase.

It looks like I have a working solution using Nippa's and Plork's examples. I used an if statement as described to sort the last example I posted. Here's the current implementation:

Python code:
eval(data, parameters, n, (n, 'date'), (n, 'close'), symbols[n], 'tk')

def eval(data, parameters, len_source, date_loc, price_loc, symbol, _type):
    failed = False
    result = []
    for n2 in parameters:
        start_date = date.today() - timedelta(days = n2.days_start)
        end_date = date.today() - timedelta(days = n2.days_end)
        
        #Checks for weekend dates; uses nearest Friday instead
        if date.weekday(start_date) == 5: 
            start_date -= timedelta(days = 1)
        if date.weekday(start_date) == 6:
            start_date -= timedelta(days = 2)
        if date.weekday(end_date) == 5:
            end_date -= timedelta(days = 1)
        if date.weekday(end_date) == 6:
            end_date -= timedelta(days = 2)
        
        count = 0 #for breaking loop once both dates determined, to save time
        #loops through the days, checking if dates match. Find a way around this?
        for n3 in range(len(data[len_source])): 
            if _type == 'tk':
                if data[date_loc[0]][n3][date_loc[1]] == str(start_date):
                    start_value = float(data[price_loc[0]][n3][price_loc[1]])
                    count += 1
                if data[date_loc[0]][n3][date_loc[1]] == str(end_date):
                    end_value = float(data[price_loc[0]][n3][price_loc[1]])
                    count += 1
                if count == 2:
                    break
            elif _type == 'yf':
                if data[date_loc[0]][n3][date_loc[1]] == str(start_date):
                    start_value = float(data[price_loc[0]][price_loc[1]])
                    count += 1
                if data[date_loc1[0]][n3][date_loc1[1]] == str(end_date):
                    end_value = float(data[price_loc[0]][price_loc[1]])
                    count += 1
                if count == 2:
                    break
        
        change = ((end_value - start_value) / start_value) * 100

        print('\n', symbol, n2.name)
        print ("change: " + str(change))
        print("start:", start_value, "end:", end_value)
        print("start:", start_date, "end:", end_date)
        
        if not n2._floor <= change <= n2._ceil:
            failed = True
            print (symbol, "failed for", n2.name)
            break

    if failed == False:
        result.append(symbol)
    return result

Dominoes
Sep 20, 2007

QuarkJets posted:

Agreed; I have no idea what you're asking for at this point. What is this most recent Python code supposed to do?
Nippa and Plork posted examples that I turned into a solution. I was wondering if there's a clean way to implement variables in code similar to the .join and %s abilities of strings.

I'm now curious if there's a way to clean up the duplicate 'if' statements in a situation like this.

Dominoes fucked around with this message at 20:43 on Apr 21, 2013

Dominoes
Sep 20, 2007

Suspicious Dish posted:

Why do you have variables named n2 and n3?
Iterators.

Dominoes fucked around with this message at 22:35 on Apr 21, 2013

Dominoes
Sep 20, 2007

QuarkJets posted:

Don't do this:

code:
 if failed == False:
        result.append(symbol)
Instead:

code:
if not failed:
        result.append(symbol)
...

And instead of a failure flag, you could just return when your failure condition is met. Plus it looks like result is never longer than length==1, so couldn't you just scrap it entirely? Like this :

Python code:
eval(data, parameters, n, \
	(n, 'date'), (n, 'close'), \
	symbols[n], 'tk')

def eval(data, parameters, len_source, \
	date_loc, price_loc, symbol, _typ):
    for n2 in parameters:
	#some code
        
        if not n2._floor <= change <= n2._ceil:
            print (symbol, "failed for", n2.name)
            return None
    return symbol
If eval returns None, then eval failed, otherwise you've got your symbol object...

...

Wait, symbol isn't ever used or modified anywhere in the code! Can't this just return True or False?
Good catches. Didn't know about the 'if not failed' boolean logic. You're right about the append being unecessary; I was mixing up code in this function and in the one that calls it. Made your change to the return logic. 'symbol' is passed as an argument. It looks like you're right that I could turn this function into a True/False return. Not sure if it's better; I'll think about it. I'd still want to pass the symbol name for the debugging prints.

edit: I like your True/False return suggestion more than returning the symbol name. Changed. Removed the "failed" variable.

Suspicious Dish posted:

Name them something better. I also see a lot of issues with your code; use more variables. Like, I see that some of your code references date_loc1, which AFAICT doesn't exist. I don't know much about your data structures, but you should be able to use zip() or something instead of the range.
The tutorials I learned from would use statements like "for parameter in parameters" or "for day in range". I don't like them because I have the phrase "parameter(s)" and "day(s)" several other places in the program, and want to make it easy to distinguish. The short names also make the code easier to read, although harder for someone else to interpret. I'm open to changing and suggestions on alternate ways of doing this. "loc1" was a typo. I'll look up zip().

Dominoes fucked around with this message at 02:22 on Apr 22, 2013

Dominoes
Sep 20, 2007

Replaced all instances of "n" and "i" iterators with names that make sense in both my programs.
Do y'all usually include __repr__ functions in classes containing instances?

Dominoes fucked around with this message at 02:22 on Apr 22, 2013

Dominoes
Sep 20, 2007

Dren posted:

Dominoes, you were talking about having data from two sources. One dataset is in json the other is in csv. Do the two data sources provide you the same data? If so, you should look at creating your own type, call it StockData. When you ingest data, load it into a StockData object. The custom code for how to read in each type of data will go into those load functions. Your code will access the StockData object. This way you don't have to write nutty custom accessor stuff (like the code you have).

This is just a suggestion, you might have a need for the way your stuff is right now.
Correct. I'm pulling real-time, and historical data from both sources. The code I posted is part of the historical data section. The real-time functions are much cleaner, since the sources let me pull data from 200 and 2500 stocks at once, so I don't have to loop through each stock. Results should be the same for each source. I don't see why that wouldn't work - I'll try to do it.


I finished my first computer program, and sent it to my coworkers tonght! It provides an interface for quickly downloading the latest version of a selection of FAA instrument procedures, and saving them as a combined PDF, or individual ones. I'm hoping to make it universal later, with the ability to select any FAA procedure, instead of just the ones I loaded.

Dominoes fucked around with this message at 02:30 on Apr 23, 2013

Dominoes
Sep 20, 2007

dantheman650 posted:

I am completely new to programming and am playing around with Python after learning some basics on CodeAcademy. I tried using Notepad++ on a friend's recommendation but it turns out getting Python code to run from it is a pain. The OP of this thread is mostly going over my head and the tutorial on setting up VIM is much more advanced and complicated than anything I need at this point. What is a good, simple IDE to write and run Python code? The wiki has a huge list and I don't know which to pick.
Download and install This Notepad++ pluigin

Run scripts with alt+shift+f5, or ctrl+alt+shift+f5 for GUI-only.

Dominoes
Sep 20, 2007

dantheman650 posted:

I downloaded the Notepad++ plugin and I have no idea what to do with it. The installation guide says to copy to the Notepad++ directory but should I copy and replace the plugin folder that is already there?
Just unzip it into the Notepad++ folder like the instructions say. Your unzip program (ie the built in one with windows) will add its files to the existing plugins directory. Using this, or double-clicking your .py file are easier, quicker methods.

Dominoes fucked around with this message at 00:08 on May 2, 2013

Adbot
ADBOT LOVES YOU

Dominoes
Sep 20, 2007

Solution to a question I posted a month ago:

Dominoes posted:

Another CSV / memory vs drive write question. If I want to download a CSV from a url, is there a way I can put the file information directly into a variable in memory? I've tried many combinations using list(), read(), csv.reader() etc, but can't find a working solution that skips writing the csv to disk, and reading it.

Code like this works:
Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        with open('drive_file.csv', 'wb') as f:
            f.write(u.read())
        
        with open('drive_file.csv', 'r') as f:
            return list(csv.reader(f))
For example, this seems like it should do what I'm asking, but doesn't.

Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        return list(csv.reader(u.read()))

Got it working by 1: Using Requests instead of urllib.request, and 2: using splitlines().

Here's what the working code looks like:
Python code:
def function():
	u = requests.get('http://somethingawful.com/COBOL.csv').text
        return list(csv.reader(u.splitlines()))
It appears that the trouble I was having was due to urllib.request's inability to pass text data instead of binary. Saving the file as an intermediary worked because you can explicitly open local files as 'r' instead of 'rb'.

Dominoes fucked around with this message at 22:15 on May 4, 2013

  • Locked thread