Re: Java is killing me! (AKA: Java for Pythonheads?)

2011-08-12 Thread Jeffrey Gaynor
One Java-eque  solution is to pass in an object that has the arguments and 
build the validity logic into that object.

So you have

public class LimitsAndLevels{
 float[] whatever = null;
 float anotherOne = 0.0; // or maybe some other overloaded value

  public float[] getWhatever(){
 isValid();
return whatever; 
  }
  public boolean hasWhatever(){
 isValid();
 return whatever != null;
  }

  public void isValid(){
   // Code to check validity
   // Probably throws an IllegalArgumentException if you need.
   // That exception extends RuntimeException, so no need to put in a 
throws list
  }
}



Then you pass this, e.g., 

public class HeavyLifter{
  public int[] quant(LimitsAndLevels args){
// acrobatics.

  }
}

Hope this helps...

Jeff


- Original Message -
From: "kj" 
To: python-list@python.org
Sent: Friday, August 12, 2011 12:02:38 PM
Subject: Java is killing me! (AKA: Java for Pythonheads?)




*Please* forgive me for asking a Java question in a Python forum.
My only excuse for this no-no is that a Python forum is more likely
than a Java one to have among its readers those who have had to
deal with the same problems I'm wrestling with.

Due to my job, I have to port some Python code to Java, and write
tests for the ported code.  (Yes, I've considered finding myself
another job, but this is not an option in the immediate future.)

What's giving me the hardest time is that the original Python code
uses a lot of functions with optional arguments (as is natural to
do in Python).  

As far as I can tell (admittedly I'm no Java expert, and have not
programmed in it since 2001), to implement a Java method with n
optional arguments, one needs at least 2**n method definitions.
Even if all but one of these definitions are simple wrappers that
call the one that does all the work, it's still a lot of code to
wade through, for nothing.

That's bad enough, but even worse is writing the unit tests for
the resulting mountain of fluffCode.  I find myself writing test
classes whose constructors also require 2**n definitions, one for
each form of the function to be tested...

I ask myself, how does the journeyman Python programmer cope with
such nonsense?

For the sake of concreteness, consider the following run-of-the-mill
Python function of 3 arguments (the first argument, xs, is expected
to be either a float or a sequence of floats; the second and third
arguments, an int and a float, are optional):

   def quant(xs, nlevels=MAXN, xlim=MAXX):
if not hasattr(xs, '__iter__'):
return spam((xs,), n, xlim)[0]

if _bad_quant_args(xs, nlevels, xlim):
raise TypeError("invalid arguments")

retval = []
for x in xs:
# ...
# elaborate acrobatics that set y
# ...
retval.append(y)

return retval

My Java implementation of it already requires at least 8 method
definitions, with signatures:

short[] quant (float[], int, float) 
  
short[] quant (float[], int   ) 
  
short[] quant (float[],  float) 
  
short[] quant (float[]) 
  

  
short   quant (float  , int, float) 
  
short   quant (float  , int   ) 
  
short   quant (float  ,  float) 
  
short   quant (float  ) 
  

Actually, for additional reasons, too arcane to go into, I also
need four more:

short   quant (Float  , Integer, Float) 
  
short   quant (Float  , Integer   ) 
  
short   quant (Float  ,  Float) 
  
short   quant (Float  ) 
  

Writing JUnit tests for these methods is literally driving me
INSANE.

Some advice on implementing and testing functions with optional
arguments in Java would be appreciated.

TIA!

kj
-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/list

Re: value of pi and 22/7

2011-03-17 Thread Jeffrey Gaynor
(pulls out doctorate in Math.) Take a circle and measure its diameter, then 
circumference (coffee cans and string are helpful). Then

pi = Circumference/diameter

approximating that is hard. It turns out that even though it *looks* like a 
nice fraction, the value that results is not (fractions of integers have the 
charming property that they always repeat, for instance 22/7 = 3.142857 142857 
142857 142857 142857... Pi does not. Again this was a very hard question only 
answered in the 18th century by Lambert, I do believe.)

It is the simple "fractional" look about pi vs. how hard it is to compute that 
drives most of the confusion about pi. The digits of pi are in effectively 
random order (each digit occur roughly 10% of the time), and to compute the nth 
one you need all the digits before it. Once upon a time (and maybe still) 
sending back and forth long strings of the digits of pi was a great way to test 
communications, since each side could look up the result in a table and tell if 
there were systematic errors. There are fun math questions, for instance, is 
there a run of a million 1's someplace in the decimal expansion of pi? Maybe 
so, but we just don't know, since we've only computed the first trillion or so 
digits. Computing pi also requires a lot of logistical organization too and 
cranking out the first several hundred million digits is still often used to 
test systems. 

FWIW my favorite approximation is 355/113. I can always seem to remember that 
one the best...

Jeff

- Original Message -
From: "kracekumar ramaraju" 
To: python-list@python.org
Sent: Thursday, March 17, 2011 11:46:25 AM
Subject: value of pi and 22/7


I tried the following 
>>> 22/7.0 
3.1428571428571428 
>>> import math 
>>> math.pi 
3.1415926535897931 
>>> 


Why is the difference is so much ?is pi =22/7 or something ? 
-- 
winning regards 
kracekumar 

-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: value of pi and 22/7

2011-03-17 Thread Jeffrey Gaynor
There are a few long strings, but have fun yourself with the pi digit searcher:

http://www.angio.net/pi/bigpi.cgi

Longest string I heard of was nine 6's in a row, so search for 6 and 
see what you get.

- Original Message -
From: "Ian Kelly" 
To: "Jeffrey Gaynor" 
Cc: python-list@python.org
Sent: Thursday, March 17, 2011 1:49:56 PM
Subject: Re: value of pi and 22/7

On Thu, Mar 17, 2011 at 11:36 AM, Jeffrey Gaynor  wrote:
> There are fun math questions, for instance, is there a run of a million 1's 
> someplace in the decimal expansion of pi? Maybe so, but we just don't know, 
> since we've only computed the first trillion or so digits.

Since pi is irrational I would be surprised if there isn't one
eventually.  Out of my own curiosity, do you know what the longest
known string of repeating digits in pi is?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Egos, heartlessness, and limitations

2011-04-14 Thread Jeffrey Gaynor
Lemme see now...




>i laid out grandiose plans for a new beginning only to have my words
>fall on deaf ears. Have we become so self absorbed as to care only for
>our status and ego and not for the community at whole? 

So you proposed a grandiose plane that is a heck of a lot of work for the 
people on this list who are trying to get their jobs done and are now 
complaining they aren't doing all that extra work for you? Really?  You 
diagnosed this as being caused by



>   * Poor Documentation or lack thereof
>   * Knowledge Hoarding
>   * Selfishness
>   * Lack of alturistic tendancies [sic!]



Again, not having people take a lot of time to personally tutor you is not 
Knowledge Hoarding. An awful lot of knowledge can't be communicated 
successfully to others in written format and requires a great deal of give and 
take, which is time consuming. That you can't just give them orders to work 
overtime at their own expense is not selfishness nor is it lack of altruism. 
You do, however, come across as a self-righteous totalitarian, who seems to 
think that having a good idea means it is incumbent on everyone else to 
implement since you are so special. No. Write a prototype that totally rocks, 
generate some enthusiasm and above all, make a tool that helps *other* people 
and they will flock to this. Said more plainly, the direction of your supposed 
altruism is 180 degrees off the mark. 

Don't know much about this topic, but boy is my BS detector going off... And 
just a tip on people skills, you will never get people to do voluntarily things 
for you (which is the real definition of power) by this sort of shtick.


-- 
http://mail.python.org/mailman/listinfo/python-list


Newbie question regarding SSL and certificate verification

2010-07-28 Thread Jeffrey Gaynor
Hi,

I am making a first large project in python and am having quite a bit of 
difficulty unscrambling various python versions and what they can/cannot do. To 
wit, I must communicate with certain services via https and am required to 
perform  certificate verification on them.

The problem is that I also have to do this under CentOS 5.5 which only uses 
python 2.4 as its default -- this is not negotiable. As near as I can tell from 
reading various posts, the https client does not do verification and there is 
no low-level SSL  support to provide a workaround. Near as I can tell from 
reading, 2.6 does include this. Am I getting this right? Is there a simple way 
to do this? More to the point, I need to know pretty darn quick if this is 
impossible so we can try and plan for it. 

So the quick question: Has anyone done certificate  verification using 2.4 and 
if so, how?

Thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question regarding SSL and certificate verification

2010-07-29 Thread Jeffrey Gaynor
Thank you! This is what I was looking for. 

A final question -- how widely is M2Crypto used? Since I will have to now pitch 
to our group that this is preferable the first questions they will ask are 
about stability, who is using it and how secure is it really, especially since 
it is at version 0.20.2 (i.e. no major release yet).

Thanks again!

Jeff

- Original Message -
From: "John Nagle" 
To: python-list@python.org
Sent: Thursday, July 29, 2010 12:08:57 AM
Subject: Re: Newbie question regarding SSL and certificate verification

On 7/28/2010 6:26 PM, geremy condra wrote:
> On Wed, Jul 28, 2010 at 4:41 PM, Jeffrey
> Gaynor  wrote:
>> Hi,
>>
>> I am making a first large project in python and am having quite a
>> bit of difficulty unscrambling various python versions and what
>> they can/cannot do. To wit, I must communicate with certain
>> services via https and am required to perform  certificate
>> verification on them.
>>
>> The problem is that I also have to do this under CentOS 5.5 which
>> only uses python 2.4 as its default -- this is not negotiable. As
>> near as I can tell from reading various posts, the https client
>> does not do verification and there is no low-level SSL  support to
>> provide a workaround. Near as I can tell from reading, 2.6 does
>> include this. Am I getting this right? Is there a simple way to do
>> this? More to the point, I need to know pretty darn quick if this
>> is impossible so we can try and plan for it.
>>
>> So the quick question: Has anyone done certificate  verification
>> using 2.4 and if so, how?
>>
>> Thanks!
>
> M2Crypto is the way to go here. I think there's an example on their
> site.

M2Crypto does that job quite well.  Installing M2Crypto tends to be
painful if you have to build it, though.  See if you can find a pre-
built version.

You then need a "cacert.pem" file, with the root certificates you're
going to trust.  You can get one from

http://curl.haxx.se/docs/caextract.html

which converts Mozilla's format to a .pem file once a week.
The actual Mozilla source file is at

http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt

but M2Crypto needs it in .pem format.

The new Python SSL module in 2.6 and later has a huge built-in
security hole - it doesn't verify the domain against the
certificate.  As someone else put it, this means "you get to
talk securely with your attacker." As long as the site or proxy
has some valid SSL cert, any valid SSL cert copied from anywhere,
the new Python SSL module will tell you everything is just fine.

John Nagle

-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyCharm

2010-10-14 Thread Jeffrey Gaynor
Yip. I'm using it and for the most part like it. But...

I used their Java IDE for years (it totally rocks, highly recommended), so I it 
is very comfortable to use PyCharm. 

One thing that bugs me in refactoring though is that renaming a method or 
variable does not necessarily work. It's supposed to track down all references 
and correctly change them, but it tends to be hit or miss. No problem though, 
since I just do a search of the files in question and do it manually. Still, 
the Java refactoring engine works very well indeed and id one of their major 
selling points. Code completion works, you can specify different Python 
versions (helpful) and there is Django support.

The debugger, though I have only had limited use for it, does seem to work well 
too.

Certainly give it a shot. The only other IDE I found that was remotely close to 
it was Komodo which costs a lot more (Jetbrains is offering a 50% off coupon as 
a promotional offer for a while.) 

Hope this helps...



- Original Message -
From: "Robert H" 
To: python-list@python.org
Sent: Wednesday, October 13, 2010 4:36:31 PM
Subject: PyCharm

Since the new IDE from Jetbrains is out I was wondering if "you" are
using it and what "you" think about it.

I have to start learning Python for a project at work and I am looking
around for options.

Bob
-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pythagorean triples exercise

2010-10-21 Thread Jeffrey Gaynor
What you want is to realize that all integer Pythagorean triples can be 
generated by a pair of integers, (i,j), j < i. The values are just (* = 
multiply, ^ = exponentiation)

a = 2*i*j
b = i^2 - j^2
c = i^2 + j^2 (hypotenuse)

So yes indeed a^2 + b^2 = c^2. This is a very ancient result, btw and used to 
be taught in public schools until recently. 

So the programming problem is to sort through these and toss out all triangles 
for which the short side (a or b, it will vary) is less than or equal to n. Or 
you could be Math-y  and use an inequality argument to find when a or b is the 
short side (bit of work).

Hope this helps...

- Original Message -
From: "Baba" 
To: python-list@python.org
Sent: Thursday, October 21, 2010 5:51:07 AM
Subject: pythagorean triples exercise

Hi everyone

i need a hint regarding the following exercise question:

"Write a program that generates all Pythagorean triples whose small
sides are no larger than n.
Try it with n <= 200."


what is "n" ? i am guessing that it is a way to give a bound to the
triples to be returned but i can't figure out where to fit in "n".

a^a + b^b = c^c is the condition to satisfy and i need to use loops
and "n" will be an upper limit of one (or more?) of the loops but i am
a bit lost. Please help me get thinking about this right.

exercise source: Java by Dissection (Ira Pohl and Charlie McDowell)

thanks

Baba
-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pythagorean triples exercise

2010-10-22 Thread Jeffrey Gaynor
As I indicated, generating such triples is easy. What you found is the edge 
case that

2*i*j = 200  => 100 = i*j

so (i,j) = (100,1) or (50,2) (25,4), (20,5) or (10,10). The maximal value are i 
= 100, j = 1. The other sides are

i^2 - j^2 = 10,000 - 1 = 

i^2 + j^2 = 10,000 + 1 = 10,001

...and there you have your figures. A real proof consists of a bit more, but 
nobody wants to read it and there is no easy way to notate it in plain text.


- Original Message -
From: "Mel" 
To: python-list@python.org
Sent: Friday, October 22, 2010 2:20:47 PM
Subject: Re: pythagorean triples exercise

MRAB wrote:
> On 22/10/2010 13:33, Baba wrote:

>> only a has an upper limit of 200
>>
> Really? The quote you gave included "whose small sides are no larger
> than n". Note: "sides", plural.

Strangely, there does seem to be a limit.  Fixing one side at 200, the 
largest pythagorean triple I have found is (200, , 10001.0).  So far my 
math has not been up to explaining why.

Mel

-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Question on multiple python environments in Apache

2010-10-22 Thread Jeffrey Gaynor
I have several different versions of a web app that run under Apache. The issue 
is that I need to have several different configurations available under several 
environments (such as Django, mod_python and plain vanilla mod_wsgi). Is there 
a simple way to get several of these to be completely independent? I thought 
about virtualenv, but it seems that I can only get one virtual environment for 
the entire server (so this just keeps it distinct from my main python install), 
rather than half a dozen. These will be on a dedicated test server so 
performance is not an issue.  

Does anyone know of a good FAQ that discusses this? 

Thanks in advance,

Jeff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on multiple python environments in Apache

2010-10-25 Thread Jeffrey Gaynor
Because that is a mess to manage, involving hacking the Apache source and 
multiple recompiles (this will have to run under CentOS). Using Python should 
be easy and not entail multiple compiles of other people's software. My main 
question boils down to the best way to get mutltiples interpreters running at a 
time or, failing that, a way to get separate threads. 

Is there an *easy* way to do this with virtualenv or virtual hosts under Apache?

- Original Message -
From: "Mario Miri" 
To: "python-list" 
Sent: Monday, October 25, 2010 2:50:55 AM
Subject: Re: Question on multiple python environments in Apache


Why wouldn't you use multiple apache instances? 


On Fri, Oct 22, 2010 at 11:28 PM, Jeffrey Gaynor < jgay...@ncsa.uiuc.edu > 
wrote: 


I have several different versions of a web app that run under Apache. The issue 
is that I need to have several different configurations available under several 
environments (such as Django, mod_python and plain vanilla mod_wsgi). Is there 
a simple way to get several of these to be completely independent? I thought 
about virtualenv, but it seems that I can only get one virtual environment for 
the entire server (so this just keeps it distinct from my main python install), 
rather than half a dozen. These will be on a dedicated test server so 
performance is not an issue. 

Does anyone know of a good FAQ that discusses this? 

Thanks in advance, 

Jeff 
-- 
http://mail.python.org/mailman/listinfo/python-list 


-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list