Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror

Comment Shared hosting conservatism (Score 1) 230

They were removed completely as of PHP 5.4.0.

That's fine so long as all users of your application are on hosts that offer PHP 5.4.0 or later. Shared hosts tend to stay on old PHP, possibly with backported security fixes, so as not to expose their customers to breaking changes. They also tend to limit the extent to which customers can configure the web server, PHP, and database. I had to wait for my annual renewal in order to move my personal web site from another host to WebFaction in order to get halfway recent PHP.

Comment Variadic SQL queries (Score 1) 230

Parameterized statements are fine if the number and order of parameters in a particular query won't change from one execution to the next. But what's the alternative to calling the database connection's escape_string method if an application needs to include a variable number of values in a query? An example is the list on the right side of SQL operator IN. What I did back when I still used MySQLi was make a single function that turns an array into a correctly escaped and parenthesized IN list using the database connection's escape_string method, and then I use this function for every IN and only IN. Is there a better practice, other than porting the whole lot to PDO MySQL all at once?

Comment average([2000000000]*4) (Score 1) 196

If your spec is "do what Java does", then Java integer division rounds toward zero, and Java integer addition wraps within (signed) 32 bits. This means the naive Python 2 implementation gets it wrong too, and we're back to square one where we're scanning the list for being all ints and using the "act like Java" method if no float or complex types are found in the array.

Comment average([10, 12.0]) (Score 1) 196

As mentioned in the spec, int list should return an int.

Please clarify the spec further: If the list contains an int and a float (such as average([10, 12.0])), should the result be returning a float or raising a ValueError? I admit that my process of requirements elicitation sounds like I'm asking a lot of nitpicky questions, but it's necessary to avoid an underspecified function. "What Python used to do prior to the addition of true division in 2.2" is still underspecified if the function is ported to any language other than Python.

So your function should return func([-10,-11]) --> (-10-11)/2 = -11 under the rules of integer division.

Until now you have left "the rules of integer division" unspecified. Floor division in Python 2.2+ rounds toward negative infinity, but C integer division rounds toward 0. So you've added to the spec.

Comment average([10, 11]) (Score 1) 196

def average(seq): return sum(seq) / len(seq)

Or in Python 3.4 or later: from statistics import mean as average

Can you implement a simple average() function in Python 3 that satisfies the specifications mentioned above?

I'm not clear what average([-10, -11]) should return under your specification. Or would it raise a ValueError? In general, the average of integers is not an integer but a rational, because integers are not closed under mean. Mean would have to promote to a type capable of representing (or at least approximating) rationals. Python has no built-in exact rational type, but it does have float.

Comment Re:Interoperating with invalid data (Score 1) 196

The program should produce an error AT THE MOMENT IT TRIES TO EXTRACT A Unicode CODE POINT. Not before, and not after.

Which leaves open the question of how best to clean up all the tens of thousands of existing records that may not be valid UTF-8. Otherwise: "This product is unavailable for purchase because its description contains invalid data. This problem has been reported to the store owner."

Comment Deallocating resources when the caller breaks (Score 1) 196

If you are writing an object designed to work with with, you do the cleanup in its __exit__() method function.

So how does the object ensure that every other object that owns it wraps its allocation with with?

sugar can be sweet. I think it's an improvement

I agree with you that with is a syntactic improvement. I was arguing about program semantics that make with necessary, especially when object lifetimes don't correspond directly to program blocks.

You would close the file inside __next__() when you hit end-of-file and right before you raise the StopIteration exception. Am I overlooking something?

You are overlooking the case in which the caller stops using the iterator before the iterator completes, such as a for loop that hits a break statement or an exception not caught within the loop.

Comment [meta] NTS, hypocrisy, and misunderstood words (Score 1) 142

I mentioned that people who call themselves Christians but then commit acts of violence, for flimsy reasons and without provocation, are not in fact practicing Christianity. Some fool cried "hehe I guss there is No True Scotsman then huh?!" while patting himself on the back fiercely.

If someone wrongfully accuses you of creating a no true Scotsman (NTS) fallacy when discussing hypocrisy among self-proclaimed Christians, here's how I'd reply: "I've always defined 'Christian' as someone who follows the teachings of Jesus of Nazareth. Anyone who claims Christianity but materially fails to practice it is something else: a 'hypocrite'. In Jesus's time, there were hypocrites in the leadership of Pharisaic Judaism, and he tore them a new anus in a speech recorded at Matthew 23." Clearly defining the goalposts early on shifts the debate from "you moved the goalposts" to "is this person really practicing?".

This infantile fevered-ego shit is killing Slashdot much faster than a shitty Beta redesign ever could hope to do. It's just far less trendy to protest it.

The NTS fallacy usually has roots in disputes over definitions. Even Scientology recognizes the problems that misunderstood words cause. One can prevent the fallacy by agreeing upon definitions before proceeding, such as "Christian == one practicing Jesus's teachings". This is an anti-NTS step that any Slashdot user can help stop, unlike forced beta for which the only cure is leaving Slashdot in favor of Soylent News or Pipedot. Right now, one can turn off beta, but once Slashdot forces it, the only course of action will be to follow reasoning analogous to Jesus's advice to body integrity identity disorder sufferers in Mark 9:45: "If [Slashdot beta] causes you to stumble, cut it off."

Comment with, try/finally, and long-lived resources (Score 1) 196

One common design pattern for ensuring that unmanaged resources get released is try/finally, which works with JVM, CLR, and CPython, but doesn't work so well with resources held longer than one method.

And this is why it is best practice in Python to use the with statement

This is explicitly a syntactic sugar for the try/finally approach (PEP 343) and shares some of its disadvantages. Like the lifetime of a resource allocated in try and cleaned up in finally, the lifetime of a resource allocated at the start of a with block and cleaned up at the end of said block is limited to the lifetime of that block. If you allocate an object in one method, such as the __init__ method of a class, and retain it, how do you ensure that it gets freed? For example, a class that reads a stream of objects out of a file may implement the iterator protocol: open the file in __init__, return an object in __next__ (next in Python 2), and close the file when?

Comment Chinese or Hindi (Score 1) 196

various internal applications and systems not needing to support more than one language

If a program supports just one language, and that one language is either standard written Chinese or Hindi, then how should it work with even one language without Unicode?

Comment Applications written in Python (Score 1) 196

Then why the hell are they downloading python - a programming language???

To run an application written in that programming language. For the same reason that\ downloads Flash Player to run SWFs and Java to run JARs and a web browser to run web applications, one downloads Python to run Python applications.

Comment Optimize for the hardware you have (Score 1) 196

There's the old parable about two programmers that are told to double the speed of their program. One guy spends a month rewriting the core in assembly and using hardware acceleration. The other guy waits 6 months and buys a new computer.

The second guy failed because the deadline was in three months. Or the second guy failed because other customers running the program on their own computers weren't likewise willing to buy a new computer just to run one program.

Slashdot Top Deals

"In order to make an apple pie from scratch, you must first create the universe." -- Carl Sagan, Cosmos

Working...