|
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
|
# ? Sep 1, 2018 23:50 |
|
|
# ? May 14, 2024 00:31 |
|
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?
|
# ? Sep 2, 2018 01:36 |
|
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. 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
|
# ? Sep 2, 2018 05:43 |
|
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 |
# ? Sep 2, 2018 20:55 |
|
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:
code:
code:
|
# ? Sep 2, 2018 21:07 |
|
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 |
# ? Sep 2, 2018 21:07 |
|
So this basically looks okay:code:
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:
code:
|
# ? Sep 2, 2018 21:19 |
|
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?
|
# ? Sep 2, 2018 21:39 |
|
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?
|
# ? Sep 2, 2018 22:53 |
|
SurgicalOntologist posted:Here's a tricky one. This SO post appears identical to your request, and has an accepted solution: Python code:
|
# ? Sep 2, 2018 23:53 |
|
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.
|
# ? Sep 3, 2018 01:25 |
|
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. 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.
|
# ? Sep 3, 2018 02:45 |
|
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. 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)
|
# ? Sep 3, 2018 07:22 |
|
Duh, thanks, to both suggestions. I thought it would, but capturing stderr didn't work either, so I'll do the second one.
|
# ? Sep 3, 2018 15:45 |
|
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:
|
# ? Sep 3, 2018 21:23 |
|
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:
Python code:
So, to use it interactively, the user would do something like: code:
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 |
# ? Sep 3, 2018 21:45 |
|
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.
|
# ? Sep 4, 2018 17:11 |
|
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 |
# ? Sep 4, 2018 17:22 |
|
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? Instead of trying to manage that I think I would just die.
|
# ? Sep 4, 2018 17:28 |
|
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.
|
# ? Sep 4, 2018 20:34 |
|
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."
|
# ? Sep 4, 2018 21:10 |
|
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.
|
# ? Sep 5, 2018 16:25 |
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?
|
|
# ? Sep 5, 2018 17:51 |
|
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 |
# ? Sep 5, 2018 18:27 |
|
Data Graham posted:Anybody have thoughts on PyCharm vs. IntelliJ+Python extensions? Is VS Code not an option?
|
# ? Sep 6, 2018 21:14 |
|
VS Code is a great editor, but it's not even the same league as PyCharm or IntelliJ as an IDE.
|
# ? Sep 6, 2018 21:18 |
|
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.
|
# ? Sep 6, 2018 22:08 |
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.
|
|
# ? Sep 7, 2018 04:23 |
|
cinci zoo sniper posted:...elbows and dildos... I have to know what this means.
|
# ? Sep 8, 2018 19:25 |
PBS posted:I have to know what this means. Me too, but I like the flow of it.
|
|
# ? Sep 8, 2018 20:41 |
|
PBS posted:I have to know what this means.
|
# ? Sep 8, 2018 21:49 |
|
CarForumPoster posted:completely elbows and dildoes PBS posted:I have to know what this means. Uncomfortably bad.
|
# ? Sep 9, 2018 01:04 |
|
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:
Hughmoris fucked around with this message at 17:41 on Sep 9, 2018 |
# ? Sep 9, 2018 17:37 |
|
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?
|
# ? Sep 9, 2018 17:48 |
|
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.
|
# ? Sep 9, 2018 17:57 |
|
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 If you don't need to do it in a loop its much faster to vectorize the key operations: Python code:
code:
|
# ? Sep 9, 2018 18:48 |
|
Spime Wrangler 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.
|
# ? Sep 9, 2018 18:59 |
|
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?
|
# ? Sep 9, 2018 19:01 |
|
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. 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.
|
# ? Sep 9, 2018 19:08 |
|
|
# ? May 14, 2024 00:31 |
|
Use np.sum instead of np.cumsum. It should just return a scalar in this case, which is all you want anyway.
|
# ? Sep 9, 2018 19:17 |