|
If I have a sub-object contained in another object, how do I reference attributes of the root object from within the sub-object? In case that isn't clear: code:
|
# ¿ Dec 1, 2009 22:02 |
|
|
# ¿ May 9, 2024 18:24 |
|
Avenging Dentist posted:No, that's for base classes, not for the owner of an instance. This is what I was afraid of. Thanks a lot, jerk.
|
# ¿ Dec 1, 2009 22:48 |
|
Avenging Dentist posted:Only a jerk would suggest that a programmer be cognizant of a language's limitations when writing software. I can't tell if you understood this or not, but I was joking when I called you a jerk.
|
# ¿ Dec 2, 2009 00:34 |
|
Avenging Dentist posted:I figured. The post was as much for the benefit of others as it was for you (lots of people seriously do think that any criticism means the critic is a jerk). Well now you've got me refactoring, and for the first time ever I'm going to work on some sort of code design before I start writing actual code. Of course, by "first time ever" I only mean the past 6 months or so which is when I started to pick up Python and programming in general. I love criticism from people much more knowledgeable than me, so bring it on. edit: ...nevermind, I'll think about this more before posting my question... Thermopyle fucked around with this message at 06:18 on Dec 2, 2009 |
# ¿ Dec 2, 2009 05:28 |
|
It's entirely possible that I'm not using objects right, but... Is it possible for an class to return a value instead of an object when it's instantiated? For example, I've got a Film class that tries to determine which movie a file is. When I instantiate the object it parses the filename and uses the API for TMDB to determine if it's got the movie name figured out and then returns an object with title, year, actors, etc. I'd like the object to just return None or False if it thinks it's not actually a movie. Of course, I could just set an attribute on the object and then check that attribute later, but I'm mainly just curious if this is possible or if I'm thinking about classes/objects the wrong way.
|
# ¿ Dec 6, 2009 19:20 |
|
Ok, so the different pieces of software I'm writing are getting close to 5k lines total, and managing all this is getting irritating. Maintaining different versions while trying out different methods to solve problems, keeping old versions around, etc is not fun. Given that I know nothing about the subject, I'm guessing I need some sort of version control system/code repository/magic. I guess this maybe isn't Python specific...but maybe it is, I don't know: What's a good solution for a developer on a Windows machine? Is this completely language agnostic, meaning I should make a new thread about it?
|
# ¿ Dec 9, 2009 01:03 |
|
What's a good heuristic for figuring out whether something would be better as a method or a function? I'm always torn about this. For example, I have this class: code:
code:
|
# ¿ Dec 16, 2009 18:49 |
|
If someone has the time to give me a critique, I've got a 70 line module here on pastebin. This module scrapes a movies sex, violence, and profanity ratings off of kids-in-mind.com. (I've got a kid and I'd like my movie organizing script to take advantage of this stuff.) I'm particularly looking for style and code organization critiques. It's hard when you're not a pro programmer and learning programming on your own in isolation to know if you're doing things the "right" way.
|
# ¿ Dec 17, 2009 22:16 |
|
Thanks guys. The reason it's a class is that I'm imitating several other third-party modules I'm using for querying stuff like IMDB and the like. Of course, now that you made me think of it, just because they use a class doesn't mean I have to... Also, that kind of stuff was much appreciated, king_kilr.
|
# ¿ Dec 17, 2009 23:04 |
|
Out of curiosity more than anything... Say I wanted to make the module I just posted a web service. You submit the movie title and year (a POST request a guess) and get back a JSON feed with the ratings. What's the simplest way to do that? Is the simplest way somewhat scalable?
|
# ¿ Dec 18, 2009 21:06 |
|
The unique items thing reminds me of one of the first things I ever did in Python that I thought at the time was clever of me.code:
|
# ¿ Dec 26, 2009 18:34 |
|
tripwire posted:Are you writing it to a file in binary mode or ascii? I struggled for like an hour with this exact problem yesterday before I remembered to use "wb" instead of "w". The sad thing is, it seems like I forget to do it every time I write something that downloads a binary file.
|
# ¿ Jan 3, 2010 18:10 |
|
Anyone have any idea what this string is?code:
code:
|
# ¿ Jan 6, 2010 05:47 |
|
Figured it out. The first was an sha1 digest, the second was an sha1 hexdigest of the same data.
|
# ¿ Jan 6, 2010 06:21 |
|
I've got an object that caches writes to a structured data file. My object has a force_update method that does the expensive writing to the file (it's a file used by a third-party application that I have to terminate before writing to and then restart when I'm done writing). Out of curiosity...is there a way to make sure the force_update method is called before the script terminates? I can of course make sure that I have obj.force_update() at the end of my script, but I'm just thinking about convenience and catching early terminations.
|
# ¿ Jan 9, 2010 20:41 |
|
king_kilr posted:Nope. I was looking around to see if there way a pure python MySQL adapter and there doesn't seem to be one (postgres has http://pybrary.net/pg8000/). You could write a perl script that executes SQL and then have Python call it (please don't do this). Done it. quote:What are my options for persistent data storage? Have you looked at shelve?
|
# ¿ Jan 16, 2010 21:12 |
|
I get some text from one web service that just drops possessive apostrophe's. For example, for the sentence "one goon's banana" it would return:code:
My current solution is to submit the text twice. Once without the apostrophe's and then again with every word ending with a "s" changed to end with a "'s". This screws up on words that end with an s but aren't a possessive noun. Any ideas on how to address this?
|
# ¿ Jan 21, 2010 06:08 |
|
Avenging Dentist posted:Well, the main problem is that what you're looking for isn't exactly a function (but it owns): I have no use for numpy, but I can see that that feature does own!
|
# ¿ Feb 8, 2010 18:01 |
|
Is there a good library for sanitizing unicode text to Windows safe filenames while using some sort of heuristics for converting characters? I have just been using this: code:
My app takes movie titles from imdb.com and uses them to name files. Some example heuristics that I would like: 1. Alien³ should be converted to "Alien 3". 2. "—" (em dash) should be converted to "-". The problem is that I keep adding special cases for all these things and it feels like there should be a better way, or at least that someone else would have already came up with a bunch of these special cases.
|
# ¿ Feb 11, 2010 23:02 |
|
Plorkyeran posted:Make sure the user is actually using a filesystem that doesn't support Unicode before you start mangling names. NTFS supports every single character other than \ / : * ? " < > |. I have no way to guarantee that it's going to be on NTFS without making that a system requirement...which I'd rather not do.
|
# ¿ Feb 12, 2010 19:39 |
|
So, I have some data that I need to put in some XML files like this. I don't really know anything about XML or parsing it with Python and my first thought is to just build a string with regular Python string manipulations and write it to file. Is there a better way to do that?
|
# ¿ Feb 17, 2010 22:12 |
|
wins32767 posted:I didn't realize you could do if statements in list comprehensions. I just checked and the 3.1.1 docs give an example of it by itself, but the 2.6.4 example is buried in a large block. Thanks for the heads up! I never realized it either until one day I just wrote an if statement in there without thinking...and it worked!
|
# ¿ Feb 23, 2010 22:56 |
|
ErIog posted:I will attest to this. Unicode always makes everything more complicated, but it's all soluble if you remember this when dealing with Unicode. I'm on a project now that requires parsing a bunch of Unicode XML, and I've had more trouble with my debug reporting than the real problem solving. Agreed. I'm writing some stuff now that takes a bunch of unicode from web services and debugging on windows is bullshit because of the ASCII console.
|
# ¿ Mar 4, 2010 03:14 |
|
Currently kicking around my head is an idea for a game server (Battlefield: Bad Company 2) administration program. I haven't really done much with threads, but I think I'll need to use them for this project. This game server sends game events like "X killed Y", "X said Y", "X joined the server". These can be at the rate of 1 every few seconds to like maybe a dozen per second. Depending on the event, my program will do different things. Log the event to MySQL, send a command to the server, email pictures of cats to random people, whatever. This "doing different things" part is what I think is going to require the use of threads so I don't miss events. So, how I'm thinking about doing this is like this: Each incoming event is appended to a list. I have a loop in another thread that is popping event_list[0] and looking up what action to do with that event in a dictionary which contains {event: action}. This loop then fires off another thread to do whatever the dict says to do and then goes back to the event_list for the next item. Any general thoughts or advice?
|
# ¿ Mar 12, 2010 22:11 |
|
king_kilr posted:Depending on the nature of actions I think an event based application (using eventlet, or twisted or something) would be much preferable to threads, that only really works if your actions tend to be IO bound (network connections, writing to files, etc.). m0nk3yz posted:I'd tend to agree with king_kilr; not that you couldn't do this with threads (you can) but event-based systems, such as gevent/eventlet/greenlet might be a better fit in the long haul. You can skip twisted and stackless though. Thanks guys. After a cursory look at eventlet and gevent I want to make sure I understand what these things do. If I understand correctly, these implement routines that while waiting on I/O return control back to let other routines that are also doing I/O to process and thus all routines get to work because all of them spend some portion of their time waiting? Scaevolus posted:Use Queue.queue instead of lists to pass information between threads. Thanks! That's another idea to consider...
|
# ¿ Mar 14, 2010 00:16 |
|
So thanks for the recommendations for Eventlet! It's awesome. Anyway, I'm receiving commands over a socket. For example, one command may be the text "!serveryell". What's a good way to execute the correct function for the !serveryell command? Right now, I have a dict with {"!serveryell": "serveryellfunc()"} and after looking up the command in the dict, I eval("serveryellfunc()"). This feels really lovely to me. I don't really want to code in a bunch of if statements with each command so that it's easier to add new commands.
|
# ¿ Mar 17, 2010 23:05 |
|
Avenging Dentist posted:
Oh yeah...duh. Thanks.
|
# ¿ Mar 18, 2010 00:07 |
|
nbv4 posted:Calling callables __call__ callcallcallcallcallcallcall As someone much less experienced than most here, and who spends a lot of time trying to figure out other people's code, I would definitely find your usage confusing. I'd be thinking "What did I miss here? Why didn't he just use ()? What's going on?!?!"
|
# ¿ Mar 18, 2010 17:27 |
|
haywire posted:Has anyone checked out gevent or denied yet? I've been playing around with Denied, it seems very nice - it isn't in that extensive server benchmark yet, but it's been getting people talking. I've been messing around with Eventlet (AFAICT, comprable to gevent) and really like it.
|
# ¿ Apr 2, 2010 22:44 |
|
Concise method of creating a new list from every nth item of a list? I seem to write multiple lines of code for this and it seems kinda ridiculous. Example: Every 3rd item from list a makes list b. a = [1, 2, 3, 4, 5, 6, 7, 8, 9] b = [3, 6, 9]
|
# ¿ Apr 9, 2010 03:08 |
|
I was not aware of that syntax with slicing. Thanks guys. Another question: alist = ['a', 'b', 'c', 'd', 'e', 'f'] Given an index, let's say 2, what's a concise, yet readable, way to create a list that begins with alist[3] and wraps around to alist[2], so I end up with: blist = ['d', 'e', 'f', 'a', 'b', 'c']
|
# ¿ Apr 9, 2010 06:37 |
|
king_kilr posted:blist = alist[idx:] + alist[:idx] God, I'm an idiot. I think I've stayed up too late.
|
# ¿ Apr 9, 2010 06:43 |
|
m0nk3yz posted:Since I'm casting my net far and wide: Because it's too hard. It may be easy, but I don't know it and don't really feel like figuring it out. Every once in awhile I'll come across something wrong in the docs, but I just move on because I don't have any clue what to do about it. I need beat over the head with information about how to fix problems that I see. For example, I'll correct things on Wikipedia every once in awhile because there's goddamn edit buttons all over the place. I've never noticed such a thing in the stdlib docs. There may be a link on each page about correcting errors, but I've never noticed it and that's the problem. (I'm not saying the docs need to be a wiki, but that Wikipedia makes it obvious how to fix mistakes.) Someone made a comment on your site comparing Python to Ubuntu, and I think that's spot on. Most of the documentation and help available to Ubuntu users is all interactive, whereas Python.org seems pretty static. Also, the reason I posted here instead of a comment on your site is among the reasons I haven't done any contributions to Python. The mental inertia involved with participating in something new (especially considering all the other things that require interaction on the internet) is fairly substantial which means that barriers to entry need to be as small as possible. This applies to some ninja expert just as much as it does to a noob. I realize I haven't said anything real concrete about what you could do yet, so here's a suggestion I've given up to 30 seconds of thought to: Mimic the Wikipedia style of having an edit button for each part of the docs. I'm guessing you don't want it to be an edit button, but perhaps a "talk" or "suggestion" button would work. Don't require a login, but make it so you can easily put in an email address for notifications of replies to your suggestion. After someone in charge agrees with what's been ironed out in the talk section, make it part of the docs.
|
# ¿ Apr 22, 2010 21:36 |
|
If I'm using a TCP socket with a server, there's not any way to tell if the server died ungracefully, correct? I'm guessing I need the server to send a heartbeat packet every so often to make sure it's still alive... Sorry if this is retarded, as I'm currently unfamiliar with sockets.
|
# ¿ Apr 24, 2010 19:56 |
|
I'm writing a small(ish) program that requires a database. I'd like end-users to be able to use either SQLite or MySQL. This is a little out of my comfort zone, but I guess SQLAlchemy is what I need to look in to? Is there anything lighter weight that I may want to consider?
|
# ¿ May 1, 2010 22:23 |
|
Haystack posted:You might also consider using Elixir with SQLalchemy. Reportedly, it's better suited for simple projects than SQLalchemy's default declarative system. This looks cool, and I'm now leaning towards using this. Thanks. The cool thing I just figured out is that SQLAlchemy supports MySQL Connector/Python (native python Mysql) which means that all this should be Eventlet compatible!
|
# ¿ May 2, 2010 16:54 |
|
UberJumper posted:Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger. I've been using Komodo Edit, which I guess is the free, stripped down version of Komodo IDE.
|
# ¿ May 9, 2010 19:05 |
|
Not that it's ever been a problem for me, but out of curiosity...are there plans to address this "issue" in future versions of Python? edit: I wanted to add that I have no problem with I/O-constrained threading. Eventlet works great! Thermopyle fucked around with this message at 03:57 on May 12, 2010 |
# ¿ May 12, 2010 03:27 |
|
m0nk3yz posted:Wow, just wow. I need to find some way of using this in real code, just to be a dick. That would be awesome. Of course, if I read it in some real code, I would track you down and stab you in the eyeball.
|
# ¿ May 20, 2010 01:02 |
|
|
# ¿ May 9, 2024 18:24 |
|
code:
code:
code:
As an example of my cluelessness, in the install command above, I don't really know if it matters what I put as the package name in the "#egg=imdbpy" part. I just thought it seemed like a good idea, and it did install... edit: As per usual, 5 minutes after I posted I figured it out. I guess since .py files are associated with the system-wide installation of python, you have to actually type "python script.py" when using pip to be sure and use the virtualenv... Thermopyle fucked around with this message at 18:27 on May 21, 2010 |
# ¿ May 21, 2010 17:50 |