|
Boris Galerkin posted:Well I just had a frustrating time figuring out what was wrong with a part of my code when it turns out it was numpy being weird. code:
|
# ? Jul 4, 2017 16:06 |
|
|
# ? May 16, 2024 18:44 |
NaN == n is False, even if n is NaN. It's in the IEEE specificiations for floating point numbers. This is occasionally inconvenient but it's the way it is - the spec is older than Python! This is true both for numpy and Python's built-in float type, though you're unlikely to encounter NaN in that case, since most operations that produce it will raise an exception first. Eela6 fucked around with this message at 16:14 on Jul 4, 2017 |
|
# ? Jul 4, 2017 16:06 |
|
People complain about the same thing in javascript all the time. It's just the standards-defined behavior we're stuck with. As for the why, I think it boils down to the limitation of the hardware and software available at the time the standard was created. See this SO answer and the related comments.
|
# ? Jul 4, 2017 16:22 |
|
Additionally, the builtin math.isnan and np.isnan work on several nan-like-types.
|
# ? Jul 4, 2017 18:47 |
|
Ok cool thanks for the info. I figured it was a spec related thing but I guess this is the first time I've ever used a thing where nan or dividing by zero doesn't crash your program.Eela6 posted:NaN == n is False, even if n is NaN. It's in the IEEE specificiations for floating point numbers. This is occasionally inconvenient but it's the way it is - the spec is older than Python! Yep I explicitly used numpy arrays here cause I figured it was easier to just handle the nan/inf values of they ever arose rather than pre-checking.
|
# ? Jul 4, 2017 20:00 |
|
Boris Galerkin posted:Ok cool thanks for the info. I figured it was a spec related thing but I guess this is the first time I've ever used a thing where nan or dividing by zero doesn't crash your program. Behold the quiet nan
|
# ? Jul 4, 2017 20:03 |
|
Malcolm XML posted:Behold the quiet nan :hmm_emoji: That sounds like a terrible thing now that I think about it.
|
# ? Jul 4, 2017 20:07 |
|
Hey guys, I am trying to learn how to program and I'm making some progress but I am constantly worried I am doing outrageously embarrassing poo poo with my code and I find I'm second guessing it a lot There's a lot of tutorials out there on how to do stuff in Python but is there a big list anywhere of things not to do? Like, "This works, but never do it"? I know about PEP 8 but I'm thinking less "follow this style" and more "what the gently caress are you doing you idiot" My current project is a character generator for AD&D - right now, I'm storing the character's information in a big dictionary, with nested dictionaries: code:
code:
tbh the constant nagging feeling that i'm doing it wrong makes programming really discouraging Mirthless fucked around with this message at 15:30 on Jul 5, 2017 |
# ? Jul 5, 2017 15:28 |
|
I think your case right now is a really good starting point for learning about classes! Unfortunately I don't have a suitable beginner guide on those on hand right now, but you could just google "classes in python" or something like that and start from there. Also: Mirthless posted:tbh the constant nagging feeling that i'm doing it wrong makes programming really discouraging Whenever you get that "There has to be a better way of doing this" feeling, you should reach out and ask. Especially if you just starting out programming. Space Kablooey fucked around with this message at 15:38 on Jul 5, 2017 |
# ? Jul 5, 2017 15:36 |
Learning to program is an iterative process. You're constantly going to go back to code you wrote a month ago and shudder at the quality of the code you wrote then - it's a natural part of the process. It's good to want to improve your code, but don't stress out too much. Every single good programmer was once a terrible programmer. There's no single answer as to how best to structure your code. What is best depends on context. However, generally speaking, deeply nested dictionaries are a 'code smell' - it might be time to refactor and start working in classes.
|
|
# ? Jul 5, 2017 15:37 |
|
HardDiskD posted:I think your case right now is a really good starting point for learning about classes! Unfortunately I don't have a suitable beginner guide on those on hand right now, but you could just google "classes in python" or something like that and start from there. I'm using classes already though maybe incorrectly? I'm using functions for things like class/race selection, then returning the value to the dictionary when i call the function, like if you pick a Half-Elf Fighter or whatever the Fighter class gets assigned to Player['Class'] and the Half-Elf class gets assigned to Player['Race'] etc code:
code:
Eela6 posted:Learning to program is an iterative process. You're constantly going to go back to code you wrote a month ago and shudder at the quality of the code you wrote then - it's a natural part of the process. It's good to want to improve your code, but don't stress out too much. Every single good programmer was once a terrible programmer. Thanks, that's heartening at least. I knew I needed to learn and practice with classes more and this was all due for a rewrite anyway, back to the drawing board
|
# ? Jul 5, 2017 15:45 |
You may find this talk by Brett slatkin educational. https://m.youtube.com/watch?v=D_6ybDcU5gc
|
|
# ? Jul 5, 2017 15:48 |
|
Mirthless posted:I'm using classes already though maybe incorrectly? In case you don't realize this, this is going to give all your Fighter instances the same stats, but different stats each time you run your program. The randint is just going to be added to those class attributes when the file is parsed on load, not each time the class is instantiated. Python code:
code:
|
# ? Jul 5, 2017 17:43 |
|
Mirthless posted:Thanks, that's heartening at least. You've got a fairly complex system going on here, you need to be able to break it down into useful generic parts you can bolt together, and handy functions that hide the mechanics and make your objects easier to work with. Ideally you'll avoid repeating the same bit of work in multiple places, and you'll abstract it to a single function that everything calls with some parameters Being able to do this takes experience, you need to spot patterns and be aware of how you can abstract them and what classes can do for you. Really you just need to dive in and try it, maybe with a simpler example (like just a human or whatever) and see how you can improve it. It won't be perfect, but realising why means you'll do better next time - That's Programming And your code looks fine, you're just bumping up against the limitations of what you know and it's getting a bit complex. There has to be a better way!
|
# ? Jul 5, 2017 17:54 |
|
Thermopyle posted:In case you don't realize this, this is going to give all your Fighter instances the same stats, but different stats each time you run your program. It's being used to create a single output file so it hasn't really come up: pre:- Race: Human - Class: Fighter - Age: 16 (Young) - Align: Chaotic Evil =============================================================================== STR: 18(50)+Hit: 1 +Dam: 3 +Encumbrance: 1350 Open Doors: 3 BB/LG: 20 CON: 10 Bonus HP / HD: 0 System Shock: 65 Resurrection: 70 DEX: 14 Bonus to AC: 0 Reaction & Missile Adjustment: 0 INT: 07 Additional Languages Known: 0 WIS: 08 (Wisdom does not provide any bonus effects for your class) CHA: 10 Maximum Henchmen: 4 Loyalty Base: 0 Social Reaction Adj: 0 =============================================================================== HD: d10 HP: 1 Base AC: 10 With Dex: 10 With Armor: 10 Total: 10 --- Saving Throws --- Paralyzation/Poison/Death: 16 Petrification/Polymorph: 17 Rod/Staff/Wand: 18 Breath Weapon: 20 Spells: 19 =============================================================================== Weapon Proficiency Slots: 4 (mark below) Non-Proficiency Penalty: -2
|
# ? Jul 5, 2017 19:16 |
|
I'll give you one tip though - those places where you're using a string literal like 'sword' or whatever? Any value you're going to reuse, add it as a constant somewhere (like SWORD = 'Sword') so you can refer to SWORD in your code for key lookups and comparisons and the like That way you can't make a typo without python going 'hmmm dunno what a SWARD is explain yourself', whereas a typo in a string literal will pass silently and cause problems until you discover it. Also helps with refactoring like renaming (only one place to change it) and code completion
|
# ? Jul 5, 2017 19:58 |
|
Mirthless posted:I'm using classes already though maybe incorrectly? It looks like your Fighter class defines ages for various Races. It might be better if a single Age value is defined under CClass (because all characters should have an age), and then Fighter and Race can modify it, maybe using if statements to check the race and class type and then adjusting age appropriately.
|
# ? Jul 5, 2017 21:17 |
QuarkJets posted:It looks like your Fighter class defines ages for various Races. It might be better if a single Age value is defined under CClass (because all characters should have an age), and then Fighter and Race can modify it, maybe using if statements to check the race and class type and then adjusting age appropriately. To be more specific, you probably want to factor this out into a function something like this: Python code:
Eela6 fucked around with this message at 21:50 on Jul 5, 2017 |
|
# ? Jul 5, 2017 21:45 |
|
Mirthless posted:
I don't have any comments about your code, I just wanted to say it's awesome to see a AD&D 2nd edition character sheet. That's my favorite edition, and writing a character generator for it in QBasic was my first "big" programming project way back in high school.
|
# ? Jul 5, 2017 22:30 |
|
In PyCharm how do I tell it to do the "optimize imports" thing but also to ignore specific lines? Basically I need to keepcode:
e: Also how do I configure the logging module to log an output only from the root process? I run my scripts with eg code:
Boris Galerkin fucked around with this message at 13:36 on Jul 6, 2017 |
# ? Jul 6, 2017 08:15 |
|
I've been using a script for a while now to launch my flask apps in debug mode.code:
code:
quote:I got the same error while developing a flask application. To solve the problem, I had to change the environmental variable from "FLASK_DEBUG=1" to "FLASK_DEBUG=0". The reason being that turning debugging on leads to threading errors. I got the solution after reading this blog Where "this blog" points to http://azaleasays.com/2014/05/01/django-logging-with-rotatingfilehandler-error/ Any ideas what might have triggered this error to start popping up?
|
# ? Jul 6, 2017 16:41 |
|
Boris Galerkin posted:In PyCharm how do I tell it to do the "optimize imports" thing but also to ignore specific lines? Basically I need to keep I seemed to recall seeing an issue on their issue tracker, and a quick search revealed my memory was correct! So, to answer your question: you vote for this issue. As an aside, generally I've found JetBrains to be fairly responsive to their issue tracker if you write good issues.
|
# ? Jul 6, 2017 17:14 |
|
Is there any easy way to change some simple json to ini format? I can't find anything, but I'd like to avoid having to try and figure out how to roll my own converter if necessary.code:
code:
|
# ? Jul 6, 2017 21:10 |
|
Look into the json and ConfigParser modules, there should be relatively little required modification (or maybe even no required modification) to the dictionary that you get from json in order for ConfigParser to write out the INI file that you want
|
# ? Jul 6, 2017 21:56 |
|
^^^ or if there's a library, hell yeah. I always forget about dictionaries Does the structure vary or get more complex than that? If it's just a bunch of objects holding arrays you might as well just do a keys, values for loop
|
# ? Jul 6, 2017 21:58 |
|
Got it working, thanks for the suggestion. The important bit code:
|
# ? Jul 6, 2017 22:59 |
|
Methanar posted:Got it working, thanks for the suggestion. It's fine code but I'd have written it like this: code:
|
# ? Jul 7, 2017 01:18 |
|
So here's a quick question, I got a bunch of nodes connected in a graph and I wanna store info and the chirality of those connections. Is there a pretty/Pythonic way I could assemble/sort lists by chirality?
|
# ? Jul 7, 2017 01:31 |
|
QuarkJets posted:It's fine code but I'd have written it like this: You can do this too Python code:
|
# ? Jul 7, 2017 01:41 |
Philip Rivers posted:So here's a quick question, I got a bunch of nodes connected in a graph and I wanna store info and the chirality of those connections. Is there a pretty/Pythonic way I could assemble/sort lists by chirality? If you just want to sort the list once, that's easy as pie: Python code:
Python code:
Eela6 fucked around with this message at 01:53 on Jul 7, 2017 |
|
# ? Jul 7, 2017 01:50 |
|
To clarify, the nodes themselves don't have a chirality, the connecting edges have a chirality (e.g. which node would I get to next if I followed the left hand or right hand rotation).
|
# ? Jul 7, 2017 01:58 |
|
is there a rule of thumb for deciding whether to use the fork or forkserver method for spawning processes when using the multiprocessing library? i'm noticing a 2x speed differential depending on the workload, and it's not consistently one method over the other.
|
# ? Jul 10, 2017 06:01 |
|
Boris Galerkin posted:Well I just had a frustrating time figuring out what was wrong with a part of my code when it turns out it was numpy being weird. code:
|
# ? Jul 10, 2017 18:47 |
|
Thermopyle posted:I seemed to recall seeing an issue on their issue tracker, and a quick search revealed my memory was correct! So, to answer your question: you vote for this issue. Thanks. I voted for it but I'm not so hopeful that it'll get fixed/added since this issue has apparently been raised since 2013. I'll just have to be careful and change my workflow a bit: untick the "optimize imports" feature in the VCS commit menu and instead just manually hitting "optimize imports" when I finish editing a file/when I remember.
|
# ? Jul 11, 2017 07:48 |
|
Has anybody noticed quirks opening a local server socket on a work laptop using VPN? This works on VPN but hangs on the WAN:code:
|
# ? Jul 19, 2017 00:19 |
|
I'm trying to set up testing for a class that used to be a namedtuple. Previous tests could simply compare equality of the namedtuples:Python code:
I've added a method to dump out public attributes as a dict and compare against that, which works, but I'm not sure that's the right solution. Is there a better or 'best practices' way set up the class or my tests to compare the values that matter? I could write separate tests for each attribute, but that seems worse and will needlessly increase the number of tests. Python code:
|
# ? Jul 21, 2017 20:33 |
You want to implement the magic method __eq__. __eq__ is implemented like this: Python code:
Python code:
here's an example. Python code:
Eela6 fucked around with this message at 21:03 on Jul 21, 2017 |
|
# ? Jul 21, 2017 20:48 |
|
I'd considered __eq__, and actually used it during the "upgrade" from the namedtuple to the class. Then, once the class __init__ changed, that broke and I needed to rethink. (I've been looking at the code for too long, so likely not thinking clearly.) Is something like this a reasonable implementation to enable the comparison of the attributes? It works, but does it "smell"? Python code:
onionradish fucked around with this message at 21:55 on Jul 21, 2017 |
# ? Jul 21, 2017 21:35 |
onionradish posted:I'd considered __eq__, and actually used it during the "upgrade" from the namedtuple to the class. Then, once the class __init__ changed, that broke and I needed to rethink. (I've been looking at the code for too long, so likely not thinking clearly.) I'm fine with it, but I like dynamic magic. I would make the following changes Python code:
Python code:
If you want to go crazy, you can do this: Python code:
Python code:
code:
Eela6 fucked around with this message at 22:57 on Jul 21, 2017 |
|
# ? Jul 21, 2017 22:32 |
|
|
# ? May 16, 2024 18:44 |
|
Ooo .. I like the contract version! Thanks! EDIT: Why return NotImplemented vs. raise NotImplementedError()? onionradish fucked around with this message at 22:44 on Jul 21, 2017 |
# ? Jul 21, 2017 22:41 |