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.
 
  • Post
  • Reply
QuarkJets
Sep 8, 2008

In general, you want the class holding a data structure to control whether or not that data structure is allocated or deleted. There can be good designs things differently, but it's still generally true, and it's probably what you want to do

The garbage collector isn't aware of memory that you've allocated in C, so to get things working correctly (such that the memory actually gets freed) you'll want to add a ctypes call to the __del__ method of the class that wraps your data structure. When the reference count for that object reaches 0, __del__ can be called and then the memory for your C data structure gets freed and everything is kosher

Adbot
ADBOT LOVES YOU

ArcticZombie
Sep 15, 2010
So, if I free the memory in the __del__ method of the wrapper class, when the reference count of an object which uses a wrapper object as an attribute reaches 0, will the garbage collector call the __del__ method of its attributes — wait I think that's irrelevant. Provided there's no other reference to that wrapper object elsewhere outside of the object which is being garbage collected, that wrapper object will also now have 0 references and will be garbage collected.

So the right thing to do is free the memory from the __del__ method of the wrapper class, and the garbage collector will do the right thing in the event an object which uses a wrapper object needs collecting, without my intervention in a __del__ method on that class?

QuarkJets
Sep 8, 2008

ArcticZombie posted:

So, if I free the memory in the __del__ method of the wrapper class, when the reference count of an object which uses a wrapper object as an attribute reaches 0, will the garbage collector call the __del__ method of its attributes — wait I think that's irrelevant. Provided there's no other reference to that wrapper object elsewhere outside of the object which is being garbage collected, that wrapper object will also now have 0 references and will be garbage collected.

So the right thing to do is free the memory from the __del__ method of the wrapper class, and the garbage collector will do the right thing in the event an object which uses a wrapper object needs collecting, without my intervention in a __del__ method on that class?

That's right; you're implicitly saying that the wrapper class owns the memory that you're declaring in ctypes, so that memory only gets cleared when that wrapper class has no remaining refernces

edmund745
Jun 5, 2010

QuarkJets posted:

If you just dump everything into the root of your file then it's going to be really difficult to implement the design that I mentioned. Use a class, this kind of problem is exactly what a class is good for. Define a main window class and a child window class. Have the main window class own an instance of the child window class. This will be easy to implement and relatively foolproof so long as you know basic python syntax...

I am attempting to do this, and it's not working. Specifically: The IDE is telling me that it cannot find the class definition. It won't even let me try to run the script.

I am using Win10, the latest version of Python and Microsoft VS Code editor.

I can import separate files that have functions on them--but that are in the same folder--just by using " import {whatever-filename} " and that works. That doesn't work for classes however.

I've looked for examples online and those are no help, since almost all of the example web pages show how to use classes that are all contained in the main file.

I am seeing some pages that say that to import classes, you must get the literal file path, even if the target file is in the same folder as the main file. Is that correct?
Like this one:
https://www.reddit.com/r/learnpython/comments/3pzo9a/import_class_from_another_python_file/
All the pages finding this are at least 2-3+ years old.
Also some note that there is a difference in how this is done for Python 3 compared to Python 2 ??? (I'm using 3, give no shits about 2)
[edit]
--I tried that Reddit way, and it doesn't work?

edmund745 fucked around with this message at 21:01 on Sep 2, 2018

edmund745
Jun 5, 2010
Okay, here is what I am doing:
test3.py is the main file. That is supposed to have an instance of window-1. Window-1 is supposed to have an instance of window-2.
window-1 file has the window-1 class.
window-2 file has the window-2 class.

code:
# main file: test3.py
import tkinter as tk 
from tkinter import ttk


import window_file_1
import window_file_2


appWindow = window_file_1.MainWindow1Class()

root = appWindow.main_window1

root.mainloop()
code:
# file: class_main_window1_ver01.py
import tkinter as tk
from tkinter import ttk

import window_file_2

class MainWindow1Class:
    
    main_window1 = tk.Tk()
    main_window1.title("Main app window #1")
    main_window1.geometry("800x600")
    
    miniWindow2 = class_sub_window2_ver01.SubWindow2Class()
    miniWindow2.sub_window2.withdraw()

    def show_childWindow2():
        global miniWindow2.sub_window2 # ????????? what line is supposed to go here ??????????
        miniWindow2.sub_window2.deiconify()

    button_tab2 = tk.Button(main_window1, 
                   text="show 2", 
                   fg="black",
                   command=show_childWindow2)
    button_tab2.place(x = 20, y = 20, width=100, height=32)

# ~~~~~~~~~~~ the end ~~~~~~~~~~~~~~
code:
# file: class_sub_window2_ver01.py
import tkinter as tk
from tkinter import ttk


class SubWindow2Class:
    
    sub_window2 = tk.Tk()
    sub_window2.title("App sub-window #2")
    sub_window2.geometry("700x500")


# ~~~~~~~~~~~ the end ~~~~~~~~~~~~~~
All these files are in the same folder. Why doesn't this work?

QuarkJets
Sep 8, 2008

Post your code e: lol beaten

You didn't import class_sub_window2_ver01, that's why the code can't find your class

QuarkJets fucked around with this message at 21:10 on Sep 2, 2018

QuarkJets
Sep 8, 2008

So this basically looks okay:

code:
# main file: test3.py
import tkinter as tk 
from tkinter import ttk


import window_file_1
import window_file_2


appWindow = window_file_1.MainWindow1Class()

root = appWindow.main_window1

root.mainloop()
This next segment has some issues. When you define a class in this way, every instance of MainWindow1Class will be using the same main window and the same subwindow. Probably what you want is to define a constructor, so that each instance of MainWindow1Class is distinct.

For classes, we use the "self" variable in each method declaration to access the properties of that class. So in the constructor (__init__) we define self.miniWindow2 and then hide it. In show_children(self), we can access that self.miniWIndow2 and tell it to show itself.

Like this:

code:
# file: class_main_window1_ver01.py
import tkinter as tk
from tkinter import ttk

# You could import the entire module, but you really just want one object 
# from the module, so let's just import that. If you wanted to import a few things 
# you'd use commas to separate them, e.g. "from module_name import x, y, z"
from class_sub_window2_ver01 import SubWindow2Class

class MainWindow1Class:
    def __init__(self):
        self.main_window1 = tk.Tk()
        self.main_window1.title("Main app window #1")
        self.main_window1.geometry("800x600")
    
        self.miniWindow2 = SubWindow2Class()
        self.miniWindow2.sub_window2.withdraw()

        self.button_tab2 = tk.Button(self.main_window1, 
                   text="show 2", 
                   fg="black",
                   command=self.show_childWindow2)
        self.button_tab2.place(x = 20, y = 20, width=100, height=32)

    def show_childWindow2(self):
        self.miniWindow2.sub_window2.deiconify()

# ~~~~~~~~~~~ the end ~~~~~~~~~~~~~~
This basically looks fine but again you want to wrap your class code in the __init__ constructor.

code:
# file: class_sub_window2_ver01.py
import tkinter as tk
from tkinter import ttk


class SubWindow2Class:
    def __init__(self):
        self.sub_window2 = tk.Tk()
        self.sub_window2.title("App sub-window #2")
        self.sub_window2.geometry("700x500")


# ~~~~~~~~~~~ the end ~~~~~~~~~~~~~~
Try that and see if there are any issues

ArcticZombie
Sep 15, 2010

QuarkJets posted:

That's right; you're implicitly saying that the wrapper class owns the memory that you're declaring in ctypes, so that memory only gets cleared when that wrapper class has no remaining refernces

Thanks. I've got another potential issue with this project but I think it first needs a bit of background. The project is basically Stockfish for Scrabble (and similar word games), implemented in C, but I also want a sort of web preview, something similar to Scrabulizer. Each of the dictionaries used in these word games is represented as a type of specialised trie, each of which can be ~200MB, totalling ~2GB. I was intending on using Python for the back end, hence why I'm writing Python wrappers for the underlying C structures and logic. Thinking about these data structures and the web server is making my head spin. These data structures are completely read-only and I need them to be persistent, shared and allow for concurrent reads, is this feasible?

SurgicalOntologist
Jun 17, 2004

Here's a tricky one.

I'm calling a module that prints some output. I'd like to intercept the output and log it instead of printing it. I tried to monkey patch sys.stdout but it didn't work. I believe the module is calling another process but I'm not 100% sure--it's a C extension and I don't know what to look for. There's no Popen, print, or sys.stdout.write at least.

Anyways, let's assume it's calling an external process. Is there any way to capture its output?

Dominoes
Sep 20, 2007

SurgicalOntologist posted:

Here's a tricky one.

I'm calling a module that prints some output. I'd like to intercept the output and log it instead of printing it. I tried to monkey patch sys.stdout but it didn't work. I believe the module is calling another process but I'm not 100% sure--it's a C extension and I don't know what to look for. There's no Popen, print, or sys.stdout.write at least.

Anyways, let's assume it's calling an external process. Is there any way to capture its output?
Outside the box: Fork the module and change the print statement to something you can use directly? May not be a good solution, depending on your use-case.

This SO post appears identical to your request, and has an accepted solution:

Python code:
import sys

stdout_ = sys.stdout #Keep track of the previous value.
sys.stdout = open('myoutputfile.txt', 'w') # Something here that provides a write method.
# calls to print, ie import your module
sys.stdout = stdout_ # restore the previous stdout.

QuarkJets
Sep 8, 2008

ArcticZombie posted:

Thanks. I've got another potential issue with this project but I think it first needs a bit of background. The project is basically Stockfish for Scrabble (and similar word games), implemented in C, but I also want a sort of web preview, something similar to Scrabulizer. Each of the dictionaries used in these word games is represented as a type of specialised trie, each of which can be ~200MB, totalling ~2GB. I was intending on using Python for the back end, hence why I'm writing Python wrappers for the underlying C structures and logic. Thinking about these data structures and the web server is making my head spin. These data structures are completely read-only and I need them to be persistent, shared and allow for concurrent reads, is this feasible?

It seems feasible. Just spitballing, the GIL prevents true concurrency in a single Python process and makes multithreading in pure Python (for performance) effectively a nonstarter, but if you have enough memory for a multiprocessing design to work (e.g. 2GB x number of workers) then it's fine.

SurgicalOntologist
Jun 17, 2004

Dominoes posted:

Outside the box: Fork the module and change the print statement to something you can use directly? May not be a good solution, depending on your use-case.

This SO post appears identical to your request, and has an accepted solution:

Python code:
import sys

stdout_ = sys.stdout #Keep track of the previous value.
sys.stdout = open('myoutputfile.txt', 'w') # Something here that provides a write method.
# calls to print, ie import your module
sys.stdout = stdout_ # restore the previous stdout.

That's exactly what I tried. It doesn't work because they're not generating the output via python's reference to sys.stdout but rather via an external process.

Maybe this is more of a Linux question. I suppose the external process inherits the file descriptors of the python interpreter that launches it? I don't have a good handle on how that stuff works.

And it's over my head to fork because it leaves the python realm into C. Its the python library pulp calling pyglpk which uses a C extension and either calls the program glpsol or uses a C interface for the same result. I don't know what to look for in a C extension that's the equivalent of either print or Popen, and even if I found it I wouldn't know how to change it.

A simpler option would be if I could capture the right file descriptor at the OS level somehow. Not sure if that's possible.

QuarkJets
Sep 8, 2008

SurgicalOntologist posted:

That's exactly what I tried. It doesn't work because they're not generating the output via python's reference to sys.stdout but rather via an external process.

Maybe this is more of a Linux question. I suppose the external process inherits the file descriptors of the python interpreter that launches it? I don't have a good handle on how that stuff works.

And it's over my head to fork because it leaves the python realm into C. Its the python library pulp calling pyglpk which uses a C extension and either calls the program glpsol or uses a C interface for the same result. I don't know what to look for in a C extension that's the equivalent of either print or Popen, and even if I found it I wouldn't know how to change it.

A simpler option would be if I could capture the right file descriptor at the OS level somehow. Not sure if that's possible.

Have you checked stderr?

If capturing stderr or stdout doesn't capture the output of that C extension for some reason then you could use just use subprocess.Popen to create a new process that invokes the module, then capture the output from that subprocess. This will 100% work even if it's a kludge (for reasons that aren't your fault)

SurgicalOntologist
Jun 17, 2004

Duh, thanks, to both suggestions.

I thought it would, but capturing stderr didn't work either, so I'll do the second one.

Sad Panda
Sep 22, 2004

I'm a Sad Panda.
I have a project that uses Selenium to perform tasks on a variety of websites. Each website is stored as Project/Websites/WebsiteName.py, and each WebsiteName.py contains class WebsiteName(Website) with Website being the template class.
I am trying to make a script that will ...
1. List the available websites (so a list of all the .py files in Project/Websites) so the user can select which website they want to interact with.
2. Display the tasks that are possible for that website. Each task is defined in class WebsiteName as def getpromos(), def checknews() or whatever (and anything but tasks are private such as def _login())
3. The user then selects which of those tasks they'd like to do. That class is then instantiated and the task run (the instantiation can happen after they pick the website I guess, it doesn't really matter)

So, create a front end which simplifies deciding which site to open and which task to perform because at the moment it is cumbersome and is basically a list of tasks such as site 1 check news, site 4 get promos.

I started trying to code this, and it started to turn very inelegant so I feel like I'm going the wrong way.
Python code:
sites = get_sites()
# Show the user a list of the possible sites and has them pick by entering the name.
for i, site in enumerate(sites):
	print(f'{i}. {site}')
pick_one = input('Which site? : ')
# If the site name starts with a number, then the python file will start with Site_ so prepend it.
if pick_one[0].isnumeric():
	pick_one = 'Site_' + pick_one
website = f'Project.Websites.{pick_one}'
# Returns the module
imported_website = importlib.import_module(website)
# Returns the class itself in the module
website_class = getattr(imported_website, pick_one)
dict = (website_class.__dict__)
# Shows tasks (available methods in that class)
for i, key in enumerate(dict.keys()):
	if key.startswith('_'):
		pass
	else:
		print(f'{i} {key}')

SurgicalOntologist
Jun 17, 2004

I have a similar program, that scrapes a variety of websites, organized into modules. Rather than an interactive print-and-respond interface, I use a command line interface, with one command to return a list of modules. The help message looks like this:

code:
scrape

Usage:
  scrape [-d] <module> [<args>...]
  scrape list
  scrape --help
  scrape --version

Options:
  -d    Print debug messages.

Every module defines a function main, so I basically do
Python code:
module = importlib.import_module(args['<module>'])
mod_main = getattr(module, 'main')
mod_main(args['<args>'])
Each module has its own command-line interface (which is launched by the main function) so it has its own help message, can also be run separately from the rest of the project, etc. So, the master command-line interface is just passing on those extra arguments to the module's command-line interface.

So, to use it interactively, the user would do something like:
code:
scrape --help  # see all the available commands
scrape list  # see all the available modules
scrape module_name --help  # see how the module works
scrape module_name --name=alice --out=out.txt  # actually run it
I use docopt for the command-line interfaces.

I highly recommend using this kind of interface rather than print-and-respond, because it can be automated, and you don't have to write your own code for printing help messages and whatnot (like you do after the "Shows tasks" comment). I prefer docopt but you could also accomplish this with click, I'm sure, which might work better with your organization of one command per method on the class.

SurgicalOntologist fucked around with this message at 21:49 on Sep 3, 2018

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Has anybody here had to deal with running Python scripts in server environments that have really old system interpreters? Have you come up with a way to juggle releases for newer interpreters with retaining the legacy one for the system?

I'm thinking of a situation more comprehensive than just me building my own interpreter. I'm thinking like a large organization that could have Python 2.7, 3.4, 3.5, 3.6 stuff all trying to coexist. For my part, I'm looking into PyEnv right now. I don't think some basic virtualenv stuff is enough.

Hadlock
Nov 9, 2004

I haven't done this yet but you can compile your code to a static binary using nuitka, or with some additional steps cpython. Then you just deploy it like a regular app with no python runtime dependency.

pip3 install -r app/requirements.txt
nuitka3 --standalone --show-progress --python-flag=no_site app/app.py

Hadlock fucked around with this message at 17:25 on Sep 4, 2018

Thermopyle
Jul 1, 2003

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

Rocko Bonaparte posted:

Has anybody here had to deal with running Python scripts in server environments that have really old system interpreters? Have you come up with a way to juggle releases for newer interpreters with retaining the legacy one for the system?

I'm thinking of a situation more comprehensive than just me building my own interpreter. I'm thinking like a large organization that could have Python 2.7, 3.4, 3.5, 3.6 stuff all trying to coexist. For my part, I'm looking into PyEnv right now. I don't think some basic virtualenv stuff is enough.

Instead of trying to manage that I think I would just die.

SurgicalOntologist
Jun 17, 2004

I always develop and deploy in conda environments. Never have to interact with the system environments at all or worry about different versions for different projects. Can even have different versions of C dependencies or whatever else in each environment.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Thermopyle posted:

Instead of trying to manage that I think I would just die.

"That's why they pay us the big bucks hyuck hyuck hyuck." :suicide:

porksmash
Sep 30, 2008
PyEnv is great, though I am not sure how to manage it on any large scale. I use it on two single-server applications where I manage everything by hand.

Data Graham
Dec 28, 2009

📈📊🍪😋



Anybody have thoughts on PyCharm vs. IntelliJ+Python extensions?

My work is trying to eradicate the former in favor of the latter, for licensing cost reasons. All us Python developers think this will suck a bunch, but not many of us have enough experience with IntelliJ to have concrete complaints about it, other than anecdotal stories that IntelliJ takes forever to launch.

Anyone care to vent?

crazysim
May 23, 2004
I AM SOOOOO GAY
I own the All Product toolkit myself. I used to use the IntelliJ+Python setup. Today I use the PyCharm setup. I last used the IntelliJ+Python setup a few years ago.

IntelliJ+Python is great if you work on projects that have more than Java and Python. Sometimes I work on a project with a mix of Ruby and Python and it's pretty great for that.

I think you're looking for some drawbacks though. For one, the UI is not as streamlined and the terminology is a bit different because IntelliJ is cross-language. For example, you can't just open a folder in IntelliJ and get going. You have to select modules and whatnot. It's because you can open a folder and it might not be just Python-related which you can assume in PyCharm. Instead of calling things "Project Interpreters", they call it an SDK in IntelliJ.

Also, because IntelliJ is a Java-first IDE, some of the options in the UI are a bit Java-specific and not relevant to Python at all. So, maybe there's a cool feature for Java exposed in the UI but you can't use it because it's Python. It's a dead menu item that'll be there always assuming you never use Java. I mean sure, some here do the whole double press shift to use the quick action/search but the GUI is a bit noisier.

Also a lot of the help is IntelliJ/Java specific. If you're a PyCharm productivity guide addict, this is a functional small loss.

I last used the IntelliJ+Python setup a few years ago so things are a bit hazy. Some of these might have changed since then. That said, everything you can do in PyCharm you can do in IntelliJ. You just need to translate, give a bit more screen real estate, and maybe click a few more times. It's more powerful, and there's a bit more responsibility, but it's not overwhelming.

crazysim fucked around with this message at 18:29 on Sep 5, 2018

Mark Larson
Dec 27, 2003

Interesting...

Data Graham posted:

Anybody have thoughts on PyCharm vs. IntelliJ+Python extensions?

Is VS Code not an option?

Thermopyle
Jul 1, 2003

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

VS Code is a great editor, but it's not even the same league as PyCharm or IntelliJ as an IDE.

CarForumPoster
Jun 26, 2013

⚡POWER⚡

Thermopyle posted:

VS Code is a great editor, but it's not even the same league as PyCharm or IntelliJ as an IDE.

My company makes us use VS Express Desktop 2015 and it is completely elbows and dildoes compared to the free PyCharm I use on personal projects.

cinci zoo sniper
Mar 15, 2013




CarForumPoster posted:

My company makes us use VS Express Desktop 2015 and it is completely elbows and dildoes compared to the free PyCharm I use on personal projects.

VS Code is just an extendable editor, not an IDE. Although VS 2017 is still elbows and dildos compared to free PyCharm, in my unholy quest to work with R and git in it.

PBS
Sep 21, 2015

cinci zoo sniper posted:

...elbows and dildos...

I have to know what this means.

cinci zoo sniper
Mar 15, 2013




PBS posted:

I have to know what this means.

Me too, but I like the flow of it.

Dominoes
Sep 20, 2007

PBS posted:

I have to know what this means.
Context, dude.

CarForumPoster
Jun 26, 2013

⚡POWER⚡

CarForumPoster posted:

completely elbows and dildoes

PBS posted:

I have to know what this means.

Uncomfortably bad.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
I was reading this blog talking about a simple Perl and Perl 6 benchmark, and I was curious how Python stacks up: http://brrt-to-the-future.blogspot.com/2018/08/a-curious-benchmark.html

Using a Raspberry Pi 3, the C code runs in 1.9 seconds. The Perl code runs in 44 seconds.

My straight-forward Python 3.4 code runs in 64 seconds. Are there any easy wins to speed this up?
Python code:
x = 0

for i in range(1, 50000001):
    x = x + (1 / i)

print(x) # 18.304749238293297

Hughmoris fucked around with this message at 17:41 on Sep 9, 2018

SirPablo
May 1, 2004

Pillbug
Is there a cloud-based IDE that people like? I do most my work with the scipy stack in Spyder for research (numpy, pandas, matplotlib, etc.). Any recommendations to look at?

velvet milkman
Feb 13, 2012

by R. Guyovich

SirPablo posted:

Is there a cloud-based IDE that people like? I do most my work with the scipy stack in Spyder for research (numpy, pandas, matplotlib, etc.). Any recommendations to look at?

Cloud9 is pretty cool, but I haven't used it for any data science type tasks so I'm not sure if there is any support for embedded dataframes/charts or whatever.

Spime Wrangler
Feb 23, 2003

Because we can.

Hughmoris posted:

I was reading this blog talking about a simple Perl and Perl 6 benchmark, and I was curious how Python stacks up: http://brrt-to-the-future.blogspot.com/2018/08/a-curious-benchmark.html

Using a Raspberry Pi 3, the C code runs in 1.9 seconds. The Perl code runs in 44 seconds.

My straight-forward Python 3.4 code runs in 64 seconds. Are there any easy wins to speed this up?
Python code:
x = 0

for i in range(1, 50000001):
    x = x + (1 / i)

print(x) # 18.304749238293297

If you don't need to do it in a loop its much faster to vectorize the key operations:

Python code:
import numpy as np
from timeit import timeit

max = 50000001

def version_a():
    """
    Pure python.
    """
    x = 0
    for i in range(1, max):
        x = x + (1 / i)
    print(x)

def version_b():
    """
    Numpy to generate reciprocals, loop to sum
    """
    xs = np.arange(1, max)
    rs = 1/xs
    x = 0
    for r in rs:
        x = x + r
    print(x)

def version_c():
    """
    Numpy to generate reciprocals and to sum
    """
    xs = np.arange(1, max)
    rs = 1/xs
    x = np.cumsum(rs)[-1]
    print(x)


n = 5

total_a = timeit(version_a, number=n)
total_b = timeit(version_b, number=n)
total_c = timeit(version_c, number=n)

print('avg time for a = {0:0.3f} seconds'.format(total_a/n))
print('avg time for b = {0:0.3f} seconds'.format(total_b/n))
print('avg time for c = {0:0.3f} seconds'.format(total_c/n))
Gives the result:
code:
avg time for a = 4.324 seconds
avg time for b = 6.554 seconds
avg time for c = 1.151 seconds
edit: please excuse my bad variable names lol

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Spime Wrangler posted:

code:
def version_c():
    """
    Numpy to generate reciprocals and to sum
    """
    xs = np.arange(1, max)
    rs = 1/xs
    x = np.cumsum(rs)[-1]
    print(x)

Thanks for this. On the Raspberry Pi 3, version_c tosses me a MemoryError but it helps to see how I could use Numpy for something like this.

It chewed through 40,000,000 in 9 seconds.

Dr Subterfuge
Aug 31, 2005

TIME TO ROC N' ROLL
I had to resist just giving a trite answer of "use numpy." Is there any other python way that comes close to the same speed?

Spime Wrangler
Feb 23, 2003

Because we can.

Hughmoris posted:

Thanks for this. On the Raspberry Pi 3, version_c tosses me a MemoryError but it helps to see how I could use Numpy for something like this.

It chewed through 40,000,000 in 9 seconds.

Yeah cumsum generates an array with every single reciprocal summed cumulatively to that point. 39,999,999 extra stored values probably aren’t helpful with limited memory.

Adbot
ADBOT LOVES YOU

Dr Subterfuge
Aug 31, 2005

TIME TO ROC N' ROLL
Use np.sum instead of np.cumsum. It should just return a scalar in this case, which is all you want anyway.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply