Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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:
class root():
    def __init__(self, var):
        self.a_root_attrib = var
        self.a_sub_obj = sub()

class sub():
    def __init__(self):
        self.root_ref = some way of referring to a_root_attrib

foobar = root('bar')
Sorry I'm such a noob.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Avenging Dentist posted:

No, that's for base classes, not for the owner of an instance.

As for the question, the answer is probably "you didn't design your code very well and should reorganize it."

This is what I was afraid of.

Thanks a lot, jerk.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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:
class Video_fileinfo():
    def __init__(self):
        self.codec = None
        self.vres = None
        self.hres = None

    def __str__(self):
        msg = []
        msg.append('---Video Info---')
        msg.append('Codec: %s' % self.codec)
        msg.append('Resolution: %sx%s' % (self.hres, self.vres))
        return '\n'.join(msg)
I've got this function which uses an external tool to get the codec and resolution info:

code:
def build_video_object(path):
    vid_obj = Video_fileinfo()
    codec_cmd = "--inform=General;%Video_Format_WithHint_List%"
    hres_cmd = "--inform=Video;%Width%"
    vres_cmd = "--inform=Video;%Height%"

    cmd = [minfo, codec_cmd, path]
    vid_obj.codec = exec_n_return(cmd).strip()

    cmd = [minfo, hres_cmd, path]
    vid_obj.hres = exec_n_return(cmd).strip()

    cmd = [minfo, vres_cmd, path]
    vid_obj.vres = exec_n_return(cmd).strip()
    return vid_obj
Would it be better to modify the class so that you pass it a path when instantiating it and use a method to get the info, or is it better to have the function set the attributes on the object?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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:
l = [1,2,3,3,4,5,5]
dupes_removed = list(dict.fromkeys(l))

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Anyone have any idea what this string is?

code:
b~\x89\xc3T\xeb\x9aW\x1c\xb1H\x84\xa0M\xc3\x94\xfd\xe6\x8bH
I think it's supposed to represent this:

code:
627E89C354EB9A571CB14884A04DC394FDE68B48
If it does, how do I convert from the first to second?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Figured it out.

The first was an sha1 digest, the second was an sha1 hexdigest of the same data.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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 :D (please don't do this).

Done it. :(

quote:

What are my options for persistent data storage?

Have you looked at shelve?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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:
one goons banana
I then need to submit this text to another web service that sometimes doesn't work as expected if apostrophe's aren't included. The owner of the web service is aware of the issue, but I expect it to be many months before it's fixed.

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Avenging Dentist posted:

Well, the main problem is that what you're looking for isn't exactly a function (but it owns):
code:
A = numpy.array(blah)
A[A>100] = 100
A[A<50] = 50
If you want to remove the values rather than clamp them:
code:
A[(A>50) & (A<100)]

I have no use for numpy, but I can see that that feature does own!

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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:
str.encode("ascii", "ignore")
However, this isn't very robust.

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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!

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Your idea sounds fine. Here's a rough sketch of how it might work (untested):


Thanks! That's another idea to consider...

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Avenging Dentist posted:

code:
d = {'!serveryell': serveryellfunc}
d['!serveryell']()

Oh yeah...duh.

Thanks.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?!?!"

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Also need to start playing around with thrift and cassandra and couchdb.

For those wanting to start using Gevent, I wrote a super simple "Hello World" app, that runs in separate greenlets:

code:
import gevent

def hello( name ):
        return "Hello, %s!" % name

if __name__ == '__main__':
        names = [ "James", "Ruth" ]
        jobs = [ gevent.spawn( hello, name ) for name in names ]
        gevent.joinall( jobs )
        for job in jobs:
                print job.value

I've been messing around with Eventlet (AFAICT, comprable to gevent) and really like it.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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]

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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']

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

king_kilr posted:

blist = alist[idx:] + alist[:idx]

God, I'm an idiot. I think I've stayed up too late.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

m0nk3yz posted:

Since I'm casting my net far and wide:

http://jessenoller.com/2010/04/22/why-arent-you-contributing-to-python/

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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!

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

UberJumper posted:

Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

I've been using Komodo Edit, which I guess is the free, stripped down version of Komodo IDE.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

code:
(test) C:\Users\Therms> pip install -e hg+[url]http://bitbucket.org/alberanid/imdbpy#egg=imdbpy[/url]

...wall of text with no obvious errors...
code:
(test) C:\Users\Therms>search_movie.py "the matrix"
Traceback (most recent call last):
  File "C:\Users\Therms\test\Scripts\search_movie.py", line 4, in <module>
    from pkg_resources import require; require('IMDbPY==4.6dev-20100521')
  File "C:\Python26\lib\site-packages\pkg_resources.py", line 2603, in <module>
    working_set.require(__requires__)
  File "C:\Python26\lib\site-packages\pkg_resources.py", line 666, in require
    needed = self.resolve(parse_requirements(requirements))
  File "C:\Python26\lib\site-packages\pkg_resources.py", line 565, in resolve
    raise DistributionNotFound(req)  # XXX put more info here
pkg_resources.DistributionNotFound: IMDbPY==4.6dev-20100521
All of the command line scripts that come with the IMDbPY package return this error. Actually using IMDbPY in my own scripts works fine. For example, the following functions correctly:

code:
>>> import imdb
>>> imdb_access = imdb.IMDb()
>>> imdb_access.search_movie("the matrix")
[<Movie id:0133093[http] title:_The Matrix (1999)_>, <Movie id:0234215[http] tit
le:_The Matrix Reloaded (2003)_>, <Movie id:0242653[http] title:_The Matrix Revo
lutions (2003)_>, <Movie id:0092106[http] title:_The Transformers: The Movie (19
86)_>, <Movie id:1392983[http] title:_The Father (2010)_>, <Movie id:0295432[htt
p] title:_The Matrix Revisited (2001) (V)_>, <Movie id:0277828[http] title:_Ente
r the Matrix (2003) (VG)_>, <Movie id:0274085[http] title:_Sex and the Matrix (2
000) (TV)_>, <Movie id:0451118[http] title:_The Matrix: Path of Neo (2005) (VG)_
>, <Movie id:0117021[http] title:_Menno's Mind (1997)_>, <Movie id:0365467[http]
 title:_Making 'The Matrix' (1999) (TV)_>, <Movie id:0390244[http] title:_The Ma
trix Online (2005) (VG)_>, <Movie id:0437137[http] title:_Crash Course (2003) (V
)_>, <Movie id:0391319[http] title:_Making 'Enter the Matrix' (2003) (V)_>, <Mov
ie id:0439783[http] title:_Return to Source: Philosophy & 'The Matrix' (2004) (V
)_>, <Movie id:0410519[http] title:_The Matrix Recalibrated (2004) (V)_>, <Movie
 id:1074193[http] title:_Decoded: The Making of 'The Matrix Reloaded' (2003) (TV
)_>, <Movie id:1499960[http] title:_The Living Matrix (2009)_>, <Movie id:038915
0[http] title:_The Matrix Defence (2003) (TV)_>, <Movie id:0438231[http] title:_
The Matrix: The Movie Special (1999) (TV)_>]
So this has something to do with how pip is installing this package. Since pip and easy_install are a big mystery to me, I was hoping someone can point me in the right direction for fixing this. It's seems obvious that it has something to do with what the install thinks is the required version for the scripts that are packaged with the package, but I don't know where it's getting that required version from, nor what it thinks the version I installed actually is.

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

  • Locked thread