|
Dr Subterfuge posted:This is basically the ideal use case for a deque and more efficient than the general purpose list. Hell yeah. Love me some data structures
|
# ? Jul 16, 2018 20:13 |
|
|
# ? Jun 1, 2024 06:04 |
Dr Subterfuge posted:This is basically the ideal use case for a deque and more efficient than the general purpose list. Whoa, this is neat.
|
|
# ? Jul 16, 2018 20:29 |
|
I learned about deques from Fluent Python, which I picked up because of reading this thread. So cheers all around.
|
# ? Jul 16, 2018 23:21 |
|
I want to debug a script that isn't throwing any exceptions. How can I get all the info like a breakpoint but with logger or traceback modules, is this possible? e.g. just enable massive amounts of debug logging so I can see how much of the script was executed before the caller bailed (trying to debug something with a lot of moving parts).
|
# ? Jul 16, 2018 23:38 |
|
Dr Subterfuge posted:I learned about deques from Fluent Python, which I picked up because of reading this thread. So cheers all around. I main Java so I have all the deques. ConcurrentLinkedDeque madam??
|
# ? Jul 17, 2018 00:59 |
|
So working with CSV files in pandas and was trying to remember if there was a way to count the number of times certain texts appear in a list and then combine it into one row with the next column listing the number of times said text value appeared.
|
# ? Jul 17, 2018 18:38 |
Hunt11 posted:So working with CSV files in pandas and was trying to remember if there was a way to count the number of times certain texts appear in a list and then combine it into one row with the next column listing the number of times said text value appeared. df.column.value_counts()
|
|
# ? Jul 17, 2018 18:43 |
|
Exact match:Python code:
Python code:
|
# ? Jul 17, 2018 18:43 |
|
I love you thread and helpful people within.
|
# ? Jul 18, 2018 01:12 |
|
Let's say I've defined in my_file.py several enums in an enum class called MyEnum. I now want to use those enums in another file. What works right now is to import the MyEnum class with code:
code:
|
# ? Jul 19, 2018 21:01 |
|
qkkl posted:Let's say I've defined in my_file.py several enums in an enum class called MyEnum. I now want to use those enums in another file. What works right now is to import the MyEnum class with You could just define the enums at the module level and then import them directly? code:
E: ignore me Enums are actually a thing now in python, I didn't actually know that NtotheTC fucked around with this message at 22:15 on Jul 19, 2018 |
# ? Jul 19, 2018 22:11 |
|
Whats the best library for map data visualisations? Particularly if my datasets have country codes
|
# ? Jul 20, 2018 14:18 |
|
Jose posted:Whats the best library for map data visualisations? Particularly if my datasets have country codes Google Maps and Flask? Edit: you could probably just write a script to spit out an HTML file.
|
# ? Jul 20, 2018 14:27 |
Jose posted:Whats the best library for map data visualisations? Particularly if my datasets have country codes Plot.ly most likely.
|
|
# ? Jul 20, 2018 14:59 |
|
Newbie question: working on optimizing a lovely half sketch I did a year ago as a generic random loot table thing for gaming. Please let me know if I’m going about this the wrong way or if there’s a better way of doing it. Here’s my current approach: Function calls random string in an array and stores it. Another function calls previously set string and calls one of several arrays based on that value. These arrays all contain a nearly identical set of strings, with certain strings removed or added based on the first chosen string. The selected array also has a random string called and stored in a new array. Based on called string, function then sets several variables that are tied to the string. Based on one of these values, it then calls for random strings in one to six of five to eleven arrays. The strings called then change one of the stored variables, and all this information is now displayed to the user. Am I going about this correctly with all these individual arrays? I feel like I’ve overcomplicated this with my lovely programming and “Well, I have a hammer . . .” approach right now. To explain what it’s for: randomly generating units in a wargame. First array is faction, which then allows or disallows unit types. Second call is to unit type, which has restrictions based off of the result of the faction variable. Third call is equipment, which is limited based on unit type, and where there is a variable number of calls (so if a unit can hold 3 “slots”, then it could make 0-3 calls to a 1-slot array, 0-1 calls to a 2-slot array if 0-1 calls were made to the 1-slot array, or 0-1 calls to the 3-slot array if 0 calls made to 1 and 2 slot arrays, plus three separate 0-slot arrays based on earlier variables can be called). Finally, it recalculates one of the values (unit cost) based on all strings used, generating text for the user. I’m hoping to expand this a bit so it can generate an entire army at once, so comparing total of unit cost values to a user-set variable is something I’m hoping to eventually add, but I’m not quite there in figuring out how to do that without creating a horrible mess of things. I also think I need to weight the 0-value calls, as I’ve had a couple tests where it managed to generate a weird outlier where it called them far too much, as well as add duplicate handling for a few things.
|
# ? Jul 23, 2018 04:19 |
|
Dr Subterfuge posted:This is basically the ideal use case for a deque and more efficient than the general purpose list. How do they implement this deque? Is it just a general dynamic array except they write the first element so that there are an equal number of unused slots before and after, rather than just after?
|
# ? Jul 23, 2018 13:48 |
|
Double linked lists with fixed length blocks, with new block data starting in the center of the block. https://github.com/python/cpython/blob/master/Modules/_collectionsmodule.c
|
# ? Jul 23, 2018 15:15 |
|
Hedningen posted:Newbie question: working on optimizing a lovely half sketch I did a year ago as a generic random loot table thing for gaming. Please let me know if I’m going about this the wrong way or if there’s a better way of doing it. You implementation is a headache to understand for a few reasons (and that's OK):
The solution is object-oriented programming and, assuming you've never experienced it before, this is a nice little problem to cut your teeth on. Hedningen posted:To explain what its for: randomly generating units in a wargame. First array is faction, which then allows or disallows unit types. Second call is to unit type, which has restrictions based off of the result of the faction variable. Third call is equipment, which is limited based on unit type, and where there is a variable number of calls (so if a unit can hold 3 slots, then it could make 0-3 calls to a 1-slot array, 0-1 calls to a 2-slot array if 0-1 calls were made to the 1-slot array, or 0-1 calls to the 3-slot array if 0 calls made to 1 and 2 slot arrays, plus three separate 0-slot arrays based on earlier variables can be called). Finally, it recalculates one of the values (unit cost) based on all strings used, generating text for the user. Here's what one approach might look like: we create classes to represent Factions, Units and Equipment. I'll sketch out the latter two. I'll assume the value you want to track depends on both the unit and its equipment. Python code:
So if we do: Python code:
The design isn't perfect, and maybe some OO wizard here can make some suggestions, but it's already much easier to conceptualise than a bunch of arrays of strings. Daviclond fucked around with this message at 22:27 on Jul 23, 2018 |
# ? Jul 23, 2018 16:51 |
|
SQLAlchemy question. I can't get a hybrid method to work. Here's the test: Python code:
code:
|
# ? Jul 23, 2018 17:32 |
|
Hedningen posted:Newbie question: working on optimizing a lovely half sketch I did a year ago as a generic random loot table thing for gaming. Please let me know if I’m going about this the wrong way or if there’s a better way of doing it. This was kinda hard to follow - which is the whole problem, right! I think you need to structure this in a way that resembles your actual game design, instead of juggling arrays. Let me see if I got this right:
I guess the most Pythonic way would be to use dictionaries? Something like Python code:
Python code:
Python code:
This isn't necessarily the best way, or even the way I'd do it (I'd prefer something like daviclond's solution because it's more familiar) but hopefully it's a start point, give you some ideas about organisation and rounding up your data into something easier to work with. Also someone tell me if this is bad, I've tried to use the more Pythony OO-lite approach with dicts and all that, but it always rubs me up the wrong way. I don't like referencing variables using typoable strings, I always want to make them global constants - define all the Units on the top level etc, then make sets of { CAR, BOAT } instead of { "CAR", "BOAT" } or whatever
|
# ? Jul 23, 2018 17:48 |
|
I am a total newbie and slowly working my way through Automate The Boring Stuff With Python. I am understanding the concepts and see wht is going on, but I am totally failing when it comes to the practice projects from Chapter 3 onwards. Once it gets to actually having to think like a coder and put together a program I completely blank and end up searching for a solution online. I can look at that solution and understand what and why, but I just can't do it myself. Would a book like Think Python help with beginning to think like a coder? Are there any other books out there to help with the thinking and logic side of coding?
|
# ? Jul 23, 2018 18:00 |
|
Forget about Python specifically for a little bit and try sketching the problem out on paper first. In plain language, write out the logic needed to solve the problem step by step, be as granular as possible. Then you can start replacing your logical steps with actual Python code.
|
# ? Jul 23, 2018 18:18 |
|
The Fool posted:Forget about Python specifically for a little bit and try sketching the problem out on paper first. Great advice, thank you!
|
# ? Jul 23, 2018 18:58 |
|
Paper is the best. Get away from the computer too, if you can
|
# ? Jul 23, 2018 19:09 |
|
Thanks both, I do find sitting in front of my computer, IDE open and just feeling completely blank. I'll go somewhere else with a pad of paper and work through it.
|
# ? Jul 23, 2018 19:52 |
|
Thank you all for the help! I’ve gotten it into a much more OO fashion right now: everything is now in classes and subclasses, which has made it much easier to debug and expand! I’ll keep tinkering and come back later when I inevitably hit another wall. As far as methods: because there’s a relatively long list of equipment and a unit was more likely to be able to use something than not, I went with adding all equipment into a list and then comparing to forbidden equipment values that each subclass of unit has, as it seemed like a better fit for what I needed. Is this a good approach? I feel like it’s better as far as maintainability and avoiding repetition.
|
# ? Jul 23, 2018 21:30 |
|
Hedningen posted:As far as methods: because there’s a relatively long list of equipment and a unit was more likely to be able to use something than not, I went with adding all equipment into a list and then comparing to forbidden equipment values that each subclass of unit has, as it seemed like a better fit for what I needed. Is this a good approach? I feel like it’s better as far as maintainability and avoiding repetition. Yeah I don't see anything wrong with the blacklist approach. I second baka kaba's suggestion of using sets instead of lists, though. It's the natural data structure if your items are unique and gives you a really nice overloaded subtract operator: Python code:
|
# ? Jul 23, 2018 22:55 |
|
Another thing you could try is a "standard equipment" collection, that way you can define sets of the base stuff plus whatever individual items you wantPython code:
The other nice thing about this approach is it might help to group stuff in other logical ways, so you can make collections of hats and shoes, or ranged and melee weapons, and define a unit (or whatever) as a recipe - add together all the collections of things you want it to use (maybe omitting some depending on the faction) and any specifics on top of that. With a good naming scheme it might make your definitions super readable. It might also be over-engineering things, so y'know - use yr judgement! Oh also frozenset is the real correct structure - a set you can't modify once it's defined. Since these are fixed definitions, it's safer to make sure you can't accidentally change them in your code and end up with subtle bugs baka kaba fucked around with this message at 01:42 on Jul 24, 2018 |
# ? Jul 24, 2018 01:40 |
|
Dead Goon posted:Thanks both, I do find sitting in front of my computer, IDE open and just feeling completely blank. I'll go somewhere else with a pad of paper and work through it. And if sketching out routines with words don't work for you because it's not your style, then go hog wild with drawing flowcharts. It's kinda all the same thing.
|
# ? Jul 25, 2018 06:52 |
|
SurgicalOntologist posted:SQLAlchemy question. Maybe something like code:
|
# ? Jul 26, 2018 03:31 |
|
Anyone have any experience with using Poetry for python projects? I recently moved over to using pipenv for everything (still a huge improvement over virtualenvwrapper or vex or whatever was used beforehand) only to learn about Poetry the other day and looking at it it does seem to have massive advantages despite being newer.
|
# ? Jul 26, 2018 08:14 |
|
Sometime in the last few releases, PyCharm's Structure view has stopped automatically updating when I switch tabs/documents and now I need to click inside it to get the list of methods etc. in the current open module. And when you click inside it, if you accidentally click on a symbol name from the previous doc, it will take you back there to the previous doc!! And then load the Structure list from the doc you actually wanted!!! I hate it and is there a way to change this please.
|
# ? Jul 26, 2018 14:53 |
|
Peeny Cheez posted:It looks like it already is a relationship. Nah, it's not a relationship. It's a hybrid method, a function that is implemented once as an instance method and once as a SQL expression classmethod. (In my DB structure I don't actually track the roster status of each player over time, just their game stats, so to see a player's current team the best way is to find the team they most recently appeared in a game for). But I'm thinking it could work better as a viewonly relationship with a custom join...
|
# ? Jul 26, 2018 15:03 |
|
Reformed Pissboy posted:Sometime in the last few releases, PyCharm's Structure view has stopped automatically updating when I switch tabs/documents and now I need to click inside it to get the list of methods etc. in the current open module. And when you click inside it, if you accidentally click on a symbol name from the previous doc, it will take you back there to the previous doc!! And then load the Structure list from the doc you actually wanted!!! I hate it and is there a way to change this please. Dunno but (I'm going off IntelliJ here) is there an option at the top of the tool window to automatically switch one view when you navigate in another? Icon looks like a little gun sight, you probably want it working one way but not the other (structure window following the editor one)
|
# ? Jul 26, 2018 15:27 |
|
NtotheTC posted:Anyone have any experience with using Poetry for python projects? I recently moved over to using pipenv for everything (still a huge improvement over virtualenvwrapper or vex or whatever was used beforehand) only to learn about Poetry the other day and looking at it it does seem to have massive advantages despite being newer. Massive advantages like what? I've stayed away from it because I don't necessarily want the best package manager thingy, I want one good enough package manager thingy to become super common and widespread...and that's most likely going to be pipenv.
|
# ? Jul 26, 2018 20:34 |
|
After this 2018.2 update my projects are no longer showing added/modified/deleted in the gutter. Is there a hidden setting I need to re-enable? Maybe I should delete all settings / preferences and try restarting fresh? edit: Pycharm mr_package fucked around with this message at 18:36 on Jul 27, 2018 |
# ? Jul 27, 2018 18:32 |
|
mr_package posted:After this 2018.2 update my projects are no longer showing added/modified/deleted in the gutter. Is there a hidden setting I need to re-enable? Maybe I should delete all settings / preferences and try restarting fresh? Sometimes after an update I have to do File > Invalidate Caches / Restart... IIRC, this empties the IDE's Local History (which is different from your VCS history), so make sure you aren't dependent upon that.
|
# ? Jul 27, 2018 19:14 |
|
That didn't do it so I deleted all the settings as per https://www.jetbrains.com/help/pycharm/2018.2/project-and-ide-settings.html (which was stupid, I should have backed them up, because I'm never going to remember the 2 or 3 useful changes I made to PyCharm's 10 trillion settings) but no change. I'm going to assume bug on OSX for now. Downgraded back to 2018.1.4 because I really rely on this feature quite a bit actually.
|
# ? Jul 27, 2018 19:53 |
|
I'm doing this in python, so I'm asking here, but this might also be a more generic "good / bad programming practices" question, as well. I have a function that uses selenium to manipulate, and BeautifulSoup to analyze, a webpage with several buttons on it. Is it better to build the function assuming that the selenium browser object will already have been created, or to explicitly pass it to the function? It hasn't mattered so far, but I figure it's better to learn these things before I form bad habits. (If it matters, there are multiple functions that I'm working on creating, but the particular one that gave rise to this takes as input a list indicating the desired state of each of 8 buttons, and returns a list of the previous state of these buttons/whether it was changed.)
|
# ? Jul 28, 2018 01:44 |
|
|
# ? Jun 1, 2024 06:04 |
|
Krakkles posted:I'm doing this in python, so I'm asking here, but this might also be a more generic "good / bad programming practices" question, as well. By "assuming that the selenium browser object will already have been created" I think you mean a global object. We generally try to avoid global objects. Pass it to your functions. Even better think about if what you really need is a class. Maybe a initialize_the_browser method on the class and then your methods can just reference self.browser. Or you can make self.browser a property and initialize it on first access. Sometimes its better to just have a module of functions that take the browser object as an argument. Or maybe you can just say gently caress it and make global browser object. Kind of depends on how Serious Business your program is.
|
# ? Jul 28, 2018 02:38 |