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
tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

duck monster posted:

But yes, the state of python on the mac is shameful at the moment. The internet is full of people saying "Use MacPython its the unofficial standard". No, theres an official standard that apple supplies, and you break your computers head if you dont

Well, part of being on OSX is that you have a full UNIX workstation at your finger tips. You should install MacPython, but do so in your home directory. I'd recommend creating a "local" directory in your home directory, and using that as the root for all of your personal software installations.

After installing python in your home directory, liberal use of virtualenv and easy_install should make things pretty trivial.

Adbot
ADBOT LOVES YOU

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

m0nk3yz posted:

I wrote an article on context managers for Python Magazine, it was in the July issue.

I love that magazine, and I loved your article. (Had no idea about the goon connection.)

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

chemosh6969 posted:

FYI: It works on linux also.

Thanks for the help from everyone.

It doesn't work on Linux at least.

A test directory:

code:
[ash@dexter tmp] $ ls
things  things2

[ash@dexter tmp] $ ls -l
total 0
-rw-r--r-- 1 ash ash 0 2008-11-06 18:53 things
-rw-r--r-- 1 ash ash 0 2008-11-06 18:54 things2
code:
[ash@dexter tmp] $ python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> p = subprocess.Popen(['ls'])
things	things2
>>> p = subprocess.Popen(['ls', '-l'])
>>> total 0
-rw-r--r-- 1 ash ash 0 2008-11-06 18:53 things
-rw-r--r-- 1 ash ash 0 2008-11-06 18:54 things2
Just as expected.


code:
>>> p = subprocess.Popen(['ls'], shell=True)
>>> things	things2

>>> p = subprocess.Popen(['ls', '-l'], shell=True)
>>> things	things2
Args were ignored.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

chemosh6969 posted:

Try it without brackets:

p = subprocess.Popen('ls -l', shell=True)

or

subprocess.call('ls -l $HOME', shell=True)

That's how the examples I found worked.
http://blog.doughellmann.com/2007/07/pymotw-subprocess.html

You mean try it as a string rather than a list?

That would be equivalent to:

code:
p = subprocess.Popen(['ls -l'], shell=True)
Which would be the other direction to the same solution as what our friend Habnabit stated here. He removed the "shell=True", you instead removed the arguments (instead passing a single string as a single argument).

tbradshaw fucked around with this message at 03:00 on Nov 7, 2008

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

chemosh6969 posted:

It at would last until the screen closes down at the end of the job which means I wouldn't see it anyway.

Okay, so maybe logging isn't useful to you. That's fine, logging isn't always useful. I can think of one use of Python where I was scripting "logon scripts" for a fleet of workstations, and without a robust logging solution, I would have just ended up with a bunch of logs scattered all over a university, not much use.

You also seem to be set in your ways, and no matter how many people tell you that you're doing a "worst practice" and you're a lovely coder, because of your attitude about it, you're not going to change.

However, for all the "new guys" reading this, there is an important lesson. It comes in two parts:

1) Logging is great, use it liberally. It's especially great for *users* to troubleshooting without having to code dive.

2) Catch exactly the exceptions you expect, no others. If logging doesn't make any sense to you in your particular application, that's fine, there's no one size-fits all solution. BUT that same rule means that a "naked except" clause is stupid. Only catch the exceptions you know can happen and you're ready to catch. Don't catch everything and assume you know everything about the world and it's methods.

Catching only the exceptions you expect is not only great for troubleshooting, it's also fantastic for code reuse. If the code you write today gets integrated into something larger, perhaps it's more appropriate for something "farther up the stack" to handle a particular exception. Exception handling in Python is very robust and extremely effective. A "naked except" clause takes all of the elegance out of the solution and makes it a kludge.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

chemosh6969 posted:

A python application for following the US primaries

edit: According to the Zen of Python, I'm right.

"Errors should never pass silently.
Unless explicitly silenced."
-- Tim Peters' Zen of Python

No, you are not explicitly silencing the error. You're just turning off error handling completely and implicitly silencing whatever error you're actually expecting. (Along with every other possible exception that you aren't expecting.)

Of the many pieces of advice you've been given on the subject, explicitly handling exceptions has been the most numerous.

e:f,b

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Janin posted:

What systems don't install Python in /usr/bin, or at least provide a symlink?

Those systems that have Python in /usr/local/bin.

Those systems with Python in /opt/python

Those systems that don't have python natively at all, but Python has been installed in the home directory of the user.

Many systems are configured in "nonstandard" ways. Maybe the simple question is "What systems can run Python when it is not installed by root?"

All of them.


Most importantly, the issue is not whether /usr/bin/python exists, it's whether you're supposed to be using it! I have Python compiled on many hosts that have an older version of Python on the system, but I have the version I want to use in ~/local/. Use "#!/usr/bin/env python" because it's not appropriate for you (as a developer) to decide that "users are stupid and will mess up their path, so I will force my code to use the system Python". "$HOME/local/bin/" comes before "/usr/bin" in my PATH environment variable for a reason!

tbradshaw fucked around with this message at 07:15 on Nov 19, 2008

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Habnabit posted:

As my latest example, I wrote a full-featured chat server in just one python expression.

Hahaha! What a stunningly Perl-like thing to do. Good times.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

functional posted:

I am using minidom. I already have the XML file in a string. How do I extract the string "dataIwant"?

May I suggest checking out Genshi for your XML parsing? Its paradigm of operating on XML as streams can be really great for extracting things. (Just give it an X-Path statement for what you want, and shazam!)

It has it's own set of pros and cons, but I've done an HTML "scraper" with it and found it to be really delightful to work with.

tbradshaw fucked around with this message at 09:02 on Nov 27, 2008

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

m0nk3yz posted:

What on earth are you talking about

Above, the code example plays "big ben".

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

tripwire posted:

Whats the idea of that? I thought it was so then when you call len() on a population object it would use the __len__ definition provided above.

Yes, that's what it is. The convention is:

  • public - a public member
  • _private - a (semi) private member. The _ demarcation is a convention to notify anyone that uses this member that they probably shouldn't be.
  • __private - a private member. The __ demarcation will trigger a name mangling of the member to make it unique to the class and very unlikely that anything that inherits, uses, or calls the member will actually do so.

An example of this would be:

code:
class A(object):
  def __do(self):
    pass


class B(A):
  def __thing(self):
    pass
  
  def stuff(self):
    self.__do()
Is actually something like:

code:
class A(object):
  def _A_do(self):
    pass


class B(A):
  def _B_thing(self):
    pass
  
  def stuff(self):
    self._B_do()
Which, as you can see from the second example, calling stuff() on B will return an error because _B_do() doesn't exist. The idea is that since you named A.__do with a __, even though B inherited the method, it's not easily called. The reason people say "THIS ISN'T PRIVATE!" is that you can definitely reach into the __dict__ of B, find the mangled name of _A_do, and call it.

However, that certainly doesn't mean that you shouldn't use __ as private. That's exactly what it's for. The real lesson here is that with the duck typing that Python uses, a "real" private member isn't nearly as useful at it seems when you're working in a static language like Java or something. So just use _private unless you find that you truly need __private.

Regarding __magic__. Things that start with double underscore and end with double underscore are lovingly called "magic" attributes. They do things like you noticed, interacting with built in functions. Generally speaking, you shouldn't ever name your own stuff __magic__, instead, you should just use those whenever you want to do something "magic" that you found in the Python documentation.

tbradshaw fucked around with this message at 20:00 on Jan 9, 2009

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Centipeed posted:

Someone needs to put together a Project Euler type website which doesn't have a focus on maths. I don't know what the possibilities would be, though. Just some simple programming problems that don't aim to take advantage of ANY particular language's abilities. Hello World 2.0 type challenges.

But... that's what makes the problems work. Sure, you could make up elaborate word problems that the solution is "implement this algorithm", but that's exactly what the math problems allow you to do without all of the extra fluff.

It's not like you need a particularly rich math background. Just look up the solutions to the problems on wiki or in a math text, and then implement the solution in your language of choice.

Ultimately, if you can't do the math described in the problems, then you're missing a prerequisite to good software engineering. Here's an opportunity to get better at both!

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

BeefofAges posted:

Not every programming problem involves implementing algorithms. It could be something like "design a system that does this:"

You might want to revisit what "algorithm" means. :D

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

CrazyPanda posted:

can someone recommend me the appropriate module for python to interface with ms access 2007. im very new to programming and databases but i know some sql. i was trying to use MYSQLdb but i dont know how to create the appropriate connection string for a database on my desktop. please let me know if mysqldb is appropriate.

While I'm uncertain what library would be best for connecting to an Access database, I'm certain that MySQLdb isn't it. MySQLdb is a module for connecting to MySQL databases, and no others.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

ShizCakes posted:

In summary, how to do I write simple web applications of Python 3 that don't require the WSGI style of doing things.

Really, the issue here is feature parity. Your interface to scripting in PHP isn't as low level as Python and you're not having to do any of the CGI yourself. In Python, WSGI is merely the more advanced CGI standard.

In the same way you let PHP do your CGI for you, you should let a Python framework do your WSGI for you. But you'll need to pick one, because Python doesn't necessarily come with a portion of web framework built-in-and-turned-on-by-default like PHP does.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

ShizCakes posted:

What I really long for is the PHP style of being able to drop a PHP file anywhere and have ONLY that php file be callable. If I want parameters, I have to pass them with ?. Is this possible?

EDIT: What I guess I am looking for is possibly a templating framework for Python? I am looking at them right now, but any pointers would be great.

Not to be pedantic, but that "style" of development really sucks. :) Even major php applications drop the "multiple entry points, multiple exit points" style and do something much more similar to what you're seeing with python web development. (Which is really just solid MVC style architecture.)

As Habnabit suggests, really, you just want to embrace it. You're not going to find an elegant solution to do things like PHP (or old ASP) where you just have templates and put your program logic in them when needed because it's a very bad development practice and not even advanced PHP developers use it.

As far as templating packages, I would humbly suggest Genshi. It's not the fastest, but it's extremely good at getting things Right.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Centipeed posted:

But this'll only work if your "title" elements are direct sub-elements of your root element, I believe.

You focused on the wrong part of the example. The important part of that example is the ".text" property that retrieves the contained text just as you want it. Find it however you want.

Centipeed posted:

XML would be so much easier to parse if you could just parse the XML in and access it this way:

That breaks because nested tags are as equally important as the text. Most importantly, in the base case tags hold other tags and *not* text. But, it's true, it would be easier for you if they ignored the breadth of XML and just focused on the small part that you're using. ;D

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Peao posted:

... Since we're recommending Python as a first language to people that ask, let's try to make the megathread as newbie-friendly as possible. ...

I don't think that you're being a dick, but I don't think that the OP necessarily needs to singly cater to "My First Programming Language" crowd. Many people that write great software in Python start with something else and move to Python.

Additionally, I must wholeheartedly champion *NO* IDE for beginning programmers. Environments that obfuscate the tool chain drastically stunt the learning process.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Lonely Wolf posted:

Since the almost univeral answer to "I want to learn how to program what should I learn?" on these forums is Python, it might not be a bad idea to at least have a section of first-time friendly links.

I would support this. A section for the "my first programming language" peeps is a solid proposal.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

spoon0042 posted:

o_O

Thanks. Do they bother to mention that in the tutorial anywhere? I may have missed it since I've just kind of been skimming it but that seems like it would be kind of important.

This isn't particular to Python. Assignment by reference vs. assignment by value is very consistent through most languages. (The same for all mainstream programming languages?)

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Janin posted:

C++ has a bizarre mixture of both, but C (everything is by-value) and Java/C#/Ruby/Python/etc (everything by-reference) are consistent.

I'm pretty sure they are all consistent. Scalars are by value, structures are by reference...

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

outlier posted:

No, but the concept of the "name" in Python is largely peculiar. All variables are effectively pointers / references, it's just that the non-mutable data obsfucates this. Looked at another way, Python's variable / data model is just like that in other languages but what has to be made explicit in other languages (passing a pointer or a reference) is implicit in Python. It can be confusing, especially if you've learnt other languages previously and it's something that most teaching material neglects or mentions later.

Could you give any examples? I can't picture this as something atypical about the labels variable system in Python. I only found it notable in regard to garbage collection. I also can't find anything that is implicit that is normally explicit. Do you mean "everyone treats the collection objects in Python as if they are scalar datatypes, and are surprised to find out they are objects?"

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Mustach posted:

My life was so much brighter before reading this. I was going to change my example to x += 2 but I can already guess that the implementation is really a pointer increment (maybe). Why would -5 be the lower bound? This is all both freakish and fascinating. Time to start reading the documentation from page 1…

This is just an optimization. The range is -5 to 256 because those are apparently the most commonly used integers and the CPython implementers found that range useful. It's a neat optimization, and really only ever comes up in situations where someone used "is" to compare integers instead of "=". A novel bit of trivia, and I only call it "just an optimization" because there's no zen-like understanding at the end of the this issue, it doesn't generalize or anything.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!
Ignore me. I read the flow of the thread wrong.

tbradshaw fucked around with this message at 23:40 on May 25, 2009

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Boblonious posted:

And by the way, this isn't for school. But regardless, I still don't want to have this project depend on an external library. I feel that this problem is small enough to write myself and not pull another dependency into it. I just wanted to make sure I was doing it right and not doing something that would end up in the coding horrors thread.

Reinventing the wheel should always be a coding horror. :(

Regardless of people's alleged problems with easy_install, that has really no bearing on the fitness of the plugin services that setuptools provides. Notably, if you're using setuptools already for installation ease (which many, many applications do) then it's not an additional dependancy at all.

I've always found the Python community's love/hate relationship with setuptools to be strange. Of course it has issues, but it is still the best we've got. The fact that setuptools is the reference implementation for moving the "good parts" into the standard library seems to be a huge admission of that fact. But package management is always this religious issue for developer and systems administrators. And for some reason, unlike every other library and application we use day to day, some people are adamant that people should avoid setuptools wholesale instead of leveraging what's good and minimizing what's bad.

I know I'm excited for what we're going to be using after setuptools. But until it's here, setuptools is still a very nice, standardized, way to handle things.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

bitprophet posted:

This is true, but I'm still not sure whether it really justifies the resulting schizophrenic API. Obviously immutable objects can't be operated on in-place, and must return a modified copy, but I don't quite see how it follows that mutable objects must return None instead of returning, say, self (i.e. modify self, then return self).

There is no reason to obfuscate what data structures are immutable and mutable. This isn't a schizophrenic API. Mutable data structures and immutable data structures behave differently. This is a good thing! They shouldn't be treated the same by developers, either.

Edit:

Maybe it would make more sense if one considers that the traditional/conventional definitions of these operations have exactly this behavior. Operations like sort on mutable data types change values in place. When extending those concepts to immutable data types, operations are similar but more expensive and now return new objects.

It might seem odd when coming from the "top down" and looking at two data types and just focusing on their similarities in syntax, but any moderately serious study in data types shows that this is exactly the behavior expected. In fact, on mutable types "None" is only returned as a convenience and isn't necessary from a data structure design standpoint.

tbradshaw fucked around with this message at 18:31 on Jun 7, 2009

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!
edit: removed extra exposition that really didn't help the conversation go anywhere

bitprophet posted:

But doesn't every function have a return value? I thought that None was the implicit return value for functions not explicitly returning something else; is that inaccurate? (edit: if you're talking very generally and not just Python syntax, I definitely understand your point, sorry.)

I was talking very generally, I didn't mean to raise that as a point of contention or anything. Yes, I'm pretty sure that every callable in Python has a return value.

edit:

Also, the choice of making strings immutable is a performance one. Strings as a mutable data type makes sense too, it just isn't implemented that way in Python.

Additionally, I see where you're coming from. Your suggestions make sense, they just come from a different perspective. These are the sorts of opinions on language design that lead developers to prefer one language over the other. Neither option is "wrong", just different. I was just trying to show a bit more fundamental/academic look at the data structures that reinforces the choice. Janin has a great "from definition" rationale for why it's the way it is. I imagine if you want more/better explanations, the python-dev mailing list is where you'd need to go. I would imagine it's one of the older threads in the archive.

tbradshaw fucked around with this message at 21:12 on Jun 7, 2009

Adbot
ADBOT LOVES YOU

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

BROHAMMER posted:

[...] AD service [...] or [...] some other way for me to bind in order to make changes to the account?

The ActiveState guys that do the ActivePython distribution provide a pretty seemless wrapper library for the native COM objects. You'll want to use that to give yourself access to the native ADS and WSH objects and work from there.

  • Locked thread