|
Sailor_Spoon posted:Breaking a string inside parentheses is preferred. Like so: I'm not sure where you got the "string inside parentheses" thing is preferred, considering PEP8 says nothing about a preferred way to split strings. Moreover, I don't think using a line span is ugly at all; PEP8 even has an example using them to split conditional statements across lines. There's nothing wrong with using a line span.
|
# ? Nov 10, 2010 19:55 |
|
|
# ? May 30, 2024 09:24 |
|
MaberMK posted:I'm not sure where you got the "string inside parentheses" thing is preferred, considering PEP8 says nothing about a preferred way to split strings. Moreover, I don't think using a line span is ugly at all; PEP8 even has an example using them to split conditional statements across lines. There's nothing wrong with using a line span. from PEP8: quote:The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is *after* the operator, not before it. Yes, that is not specifically addressing strings, but those are still on lines, aren't they? I do tend to use backslashes in long boolean expressions, but never feel good about it.
|
# ? Nov 10, 2010 20:07 |
|
Ahernia posted:I'm trying to use Tkinter's Canvas to produce a vector map of Great Britain that you can pan around in, and zoom into. What you're looking for is the Affine Transformation. It doesn't really use much more math than you're already doing, but the wiki description is pretty mathspeak-heavy. Think of it like, whenever you pan or zoom the map you find two points in the X-direction and two points in the Y-direction on both the map coordinates and the view coordinates, and fit a line (ala y = m*x+b) to each. Like: code:
So you get one equation y_view = m1*x_map + b1 and one equation x_view = m2*y_map + b2. Then to transform a point [x_map, y_map] from map coordinates to the view coordinates you just plug in your map coords and out pop the view coords. This will scale, translate, and skew a set of coordinates. Then every time you change the location of the view, redo that operation to get a new set of m's and b's. I hope that helps, and that my math is correct. (I'm on no sleep and in a bit of a hurry so I didn't check anything) You might also look into the Basemap toolkit for matplotlib i mentioned on the last page. Not sure if it'll fit your needs, but it's a readymade mapping system.
|
# ? Nov 10, 2010 20:45 |
|
Sailor_Spoon posted:from PEP8: I stand corrected. I searched the document for 'string' and variations thereof but came up with nothing. I'll remember that one.
|
# ? Nov 10, 2010 21:02 |
|
Spime Wrangler posted:
So you initially store an (x,y) value from the unmoved coordinate space, then every time the canvas got moved/scaled, find an affine transformation between the initial (x,y) and the newest (x,y). And that would be valid for all 'old' (x,y) values in the coordinate space?
|
# ? Nov 10, 2010 21:41 |
|
Well, the way I'd do it would be to have a map coordinate space that corresponds to real world locations and, unless it became a big performance hit, reproject the map data each time the view moved. For just translations I bet you're just fine with a relative method, but for stuff like scaling your probably going to have an easier time recalculating from the map coordinates. But if some one has a better idea please say so cus my experience is mostly limited to gis and static displays. However, I think you definitely want to keep a master set of data in the map coordinates, and work from there, especially if you ever want to interface with other datasets, map software, or export any data for other people to use.
|
# ? Nov 10, 2010 22:22 |
|
That's probably what I will end up doing (just transforming everything that could be displayed), it just seemed like something I should be able to figure out. I'm probably naively assuming the maths isn't complex. I guess it's solving a recursive function maybe? It's just so frustrating that movement is so easy to understand (just keep summing your offsets), and scaling is so hard.
|
# ? Nov 10, 2010 23:07 |
|
This is regarding namespace. If I have a instance, say primaryclass, and it has a variable called self.var, and from within that instance I open another instance, say secondaryclass, when I'm in secondaryclass, how do I reference that self.var, do I have to pass it as a argument?
|
# ? Nov 10, 2010 23:25 |
|
echinopsis posted:This is regarding namespace. If I have a instance, say primaryclass, and it has a variable called self.var, and from within that instance I open another instance, say secondaryclass, when I'm in secondaryclass, how do I reference that self.var, do I have to pass it as a argument? Yup. That's often the main purpose of an __init__ method. code:
|
# ? Nov 11, 2010 07:00 |
|
echinopsis posted:This is regarding namespace. If I have a instance, say primaryclass, and it has a variable called self.var, and from within that instance I open another instance, say secondaryclass, when I'm in secondaryclass, how do I reference that self.var, do I have to pass it as a argument? well, you can pass the parent object as an argument class Foo(object): def __init__(self,parent) But really what are you trying to do exactly? break encapsulation
|
# ? Nov 11, 2010 07:48 |
|
Ahernia posted:That's probably what I will end up doing (just transforming everything that could be displayed), it just seemed like something I should be able to figure out. I'm probably naively assuming the maths isn't complex. I feel your pain, having had to convert from a world space with +y being down and +z being left to screen space with +x being down and +y being right. (Minecraft ) Some tips: Three floats can represent the state of your view. center_x, center_y -- where the center of the map is, in world coordinates zoom -- the scale level. Less is more zoomed out. Then given canvas_width and canvas_height You can calculate where the corners of the canvas are pointing at, in world coordinates: map_topleft_x = center_x - width / (zoom * 2) map_topleft_y = center_y - height / (zoom * 2) map_bottomright_x = center_x + width / (zoom * 2) map_bottomright_y = center_y + height / (zoom * 2) Say the user drags the map right by 50px on the screen: center_x -= 50. / zoom Does that help at all?
|
# ? Nov 12, 2010 02:28 |
|
Alright, I wrote a program for Minecraft to measure how many blocks you will need for a project. Stacks as well. Its my first real program, so any criticism or tips would be awesome.code:
Aafter fucked around with this message at 07:13 on Nov 12, 2010 |
# ? Nov 12, 2010 07:08 |
|
Aafter posted:EDIT: After reviewing it just now, I realized that I could put 'stacks' up top instead of typing it over and over. You're asking for width/length in every case, which can be put before the measurement check. The stacks part needs to go after, as blocks has no value until after the measurement check. code:
Carthag Tuek fucked around with this message at 07:56 on Nov 12, 2010 |
# ? Nov 12, 2010 07:53 |
|
Consider separating out the formulas used to calculate blocks needed. I a Pythonic way to do so would be to create a dictionary where the keys are strings representing valid answers to the "Which calculation do you want?" question and the values are the functions themselves. For example:code:
code:
|
# ? Nov 12, 2010 08:16 |
|
DirtyDiaperMask posted:The real advantage to this sort of approach is that it makes it easier to extend the program if you need to add more calculation. Say, one that gives the number of non-visible interior cubes(or whatever, I don't play Minecraft). You simply add a function for it to the dictionary, rather than having to add on another case to your lengthy if-elif chain. code:
... I'm kidding, no need to use epydocs. edit: made more pretentious Lurchington fucked around with this message at 16:56 on Nov 12, 2010 |
# ? Nov 12, 2010 16:44 |
|
Oh java, how I've missed you.
|
# ? Nov 12, 2010 18:15 |
|
wins32767 posted:Oh java, how I've missed you. Now there's a horror.
|
# ? Nov 12, 2010 18:38 |
|
Lurchington posted:
I've recently discovered that you can generate new class definitions dynamically, just as you can with functions. So maybe we can cram that in there? Something like code:
|
# ? Nov 12, 2010 19:24 |
|
Here is this dumb thing I made for my first web app. When I say dumb, I mean really dumb, so don't blame me for stealing the ten seconds of your life it took to use. Written in Python (obv), utilizing Python Imaging Library and Flask. It's not perfect or perfectly secure but I'm pretty happy with it! There are a bunch of ideas I have for it that I will work on intermittently but I think it does about what I want it to do; I'm looking for my next project now. edit: Here's the source. code:
RobotEmpire fucked around with this message at 16:54 on Nov 14, 2010 |
# ? Nov 14, 2010 16:50 |
|
I have an image recognition module that uses numpy and nupic that was created and left behind by an old employee that I need to get working. Unfortunately I know little to nothing about python and after a lot of screwing around with PYTHONPATH this is as far as I can get:code:
Sindai fucked around with this message at 01:27 on Nov 16, 2010 |
# ? Nov 16, 2010 01:24 |
|
Try reinstalling numpy. sudo apt-get install python-numpy (yes the multiarray.so is what numpy should be seeking (and finding) normally) if you go into the python shell and type > import numpy.core > numpy.core.multiarray What does it say? RobotEmpire fucked around with this message at 05:09 on Nov 16, 2010 |
# ? Nov 16, 2010 05:07 |
|
Sindai posted:I have an image recognition module that uses numpy and nupic that was created and left behind by an old employee that I need to get working. Unfortunately I know little to nothing about python and after a lot of screwing around with PYTHONPATH this is as far as I can get: It looks like you're using Python 2.5 with that script and the multiarray.py you have is for Python 2.6
|
# ? Nov 16, 2010 05:23 |
|
Often when using a for loop to iterate through a list, I often want to use the iteration amount for something, so I end up changing something nice likecode:
code:
|
# ? Nov 16, 2010 23:39 |
|
use `enumerate`
|
# ? Nov 16, 2010 23:53 |
|
Thanks!
|
# ? Nov 17, 2010 00:14 |
|
I'm going to replace foo/bar/baz with eachdick/backofdicks/"comes with two hairy balls" from here forward.
|
# ? Nov 17, 2010 13:36 |
|
I have a question that's bugging the poo poo out of me. I'm writing a script to lightly mess with XML data that's the output of a horrible program that I have no way of controlling. I'm using ElementTree because it's the only XML library that doesn't make me want to tear my eyes out. I'm loving it except for the part where the output isn't being accepted back into the horrible program I mentioned before. The way this horrible program handles XML is really dumb, but I can't change it. Its tags are case sensitive, it doesn't support the empty closing tag(<tag />) format that ElementTree spits out. So I read the documentation a little bit, and I switch the method to spitting out html. ElementTree in this instance does something equally stupid in this mode. It gives me my verbose empty tags, but it makes every drat closing tag lowercase so that the opening tags no longer match the closing tags. quote:<PrintCopies>1</printcopies> Why the gently caress would it do this? Why does it care about the case of my tags? If it does care, then why does it only care about my closing tags? I dug through the ElementTree source code a little bit, and I found the line I need to change. Is there an elegant way I can override a single function of a module or do I just need to bite the bullet, fork ElementTree into my project directory, patch it, and go on living my life? ErIog fucked around with this message at 17:43 on Nov 17, 2010 |
# ? Nov 17, 2010 17:39 |
|
ErIog posted:I have a question that's either going to make me sound really smart or really dumb. The best option is to submit the patch to the project and hope it gets included/they tell you how to manually disable it.
|
# ? Nov 17, 2010 17:42 |
|
Jonnty posted:The best option is to submit the patch to the project and hope it gets included/they tell you how to manually disable it. I should have been more specific in my post. Is there an elegant way around this that doesn't require me forking my own version of ElementTree so that I can get my script to work today? If there isn't then that's fine, it won't take me too long. I just want to make sure there's not something simple I'm missing about how Python works that would allow me to override just this one function from a module. edit: Thankfully Python is properly OOP so I was able to monkey patch it a lot easier than I thought. I cut and pasted the _serialize_xml function into my script, renamed it to erlog_serialize_xml, and then did: code:
ErIog fucked around with this message at 18:21 on Nov 17, 2010 |
# ? Nov 17, 2010 17:51 |
|
The real answer is that you want to be using lxml.
|
# ? Nov 18, 2010 04:14 |
|
Scaevolus posted:The real answer is that you want to be using lxml. I just wish it was simple to get the latest stuff on windows. Oh well.
|
# ? Nov 18, 2010 20:31 |
|
I've got a bunch of functions I'm gonna stick in a module to clean up my code, but one of the functions relies on a value I define at the start of the "real" piece of code, but it complains it's not global. I don't want to resign myself to putting the define in the module but I can't think of an alternative..
|
# ? Nov 19, 2010 09:31 |
|
Can anyone show a simple example of simplejson? I can't seem to wrap my head around it. All I want to do is take a str object that contains some JSON, and parse it For instance: code:
code:
|
# ? Nov 19, 2010 12:41 |
|
Yakattak posted:
You're using dumps() to dump to a string but then using load() which reads from a file-stream like object. Use loads() since it's a string and all should be well.
|
# ? Nov 19, 2010 12:44 |
|
also, remember that python has built in module support for json (using the json module in 2.6) with the same interface. On the other hand, cjson is faster but has a completely different interface. Yay.
|
# ? Nov 19, 2010 15:36 |
|
Lurchington posted:also, remember that python has built in module support for json (using the json module in 2.6) with the same interface. On the other hand, cjson is faster but has a completely different interface. Yay. Yeah but I heard simplejson is the fastest, other than cjson.
|
# ? Nov 19, 2010 16:18 |
|
Lurchington posted:also, remember that python has built in module support for json (using the json module in 2.6) with the same interface. On the other hand, cjson is faster but has a completely different interface. Yay. Yakattak posted:Yeah but I heard simplejson is the fastest, other than cjson. cjson is terrible / buggy / worthless; do not use it because it will irreversibly mangle anything you put through it just do this: code:
|
# ? Nov 19, 2010 17:06 |
|
I'm working on a web project that chews threw ~1MB of JSON a every few seconds, and someone decided that every speed gain for JSON was worth it; thus, cjson. However I guess I haven't noticed the mangling part of it. If jsonlib is faster than stdlib, and uses the same interface, I'd be interested.
|
# ? Nov 19, 2010 17:24 |
|
Lurchington posted:I'm working on a web project that chews threw ~1MB of JSON a every few seconds, and someone decided that every speed gain for JSON was worth it; thus, cjson. However I guess I haven't noticed the mangling part of it. I recommend people use the stdlib instead, since it's better supported and is under active development, but feel free to at least try mine out. It's been a while since I benchmarked it, but IIRC the speed difference is only like 5% and depends heavily on what types of data you're parsing. You might also be interested in py-yajl, which is a binding to the event-based YAJL library. It's comparable to simplejson/jsonlib in speed, and if you need to you can write your own custom extension module to call YAJL directly and go even faster. You can't beat cjson for speed. But it also has so many bugs that IMO it's just not worth considering a "JSON library"; more like a "hosed-up pseudo-repr() lib" Here's a short list of some of its bugs that I've found: quote:1. cjson.decode('1')
|
# ? Nov 19, 2010 18:14 |
|
|
# ? May 30, 2024 09:24 |
|
Janin: what license are python libs typically released under? If GPL is ok, you could re-wrap the C from Perl's JSON::XS, which is fast and pretty thoroughly tested
|
# ? Nov 19, 2010 18:19 |