Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
I'm not sure what the point of any of that is;  you're making a simple

> problem complex.  If you're wanting to accomplish the task without using
> any of the itertools stuff, why not just:
>
>
> current = 10**9
> lim = 10**10
> while current < lim:
>  print current   #or write to file, or whatever
>  current += 1
>
> Rough draft, in a hurry to try and help, and would have gotten a little
more zen elegance if it were for a client.

Plus the OP stated he needed a list. What's that list of ints for? Unless
this is benchmarking, they might be in need of an unordered list of ints in
which there are 10**9 different random values that need to have the
proverbial needle(or 2, or 3) in a hey stack found.

I thought there might be a reason, hopefully, other than trying to find the
max range you could count to.

>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
I could comment this better.

We have a function, iterToHighNum, that takes two parameters: increment and
high_num.
increment is how long the list of numbers being added to the old increment
list of ints, to create a shorter int list that maintains the sequential
count, looking for the high number you're trying to reach.

I guess this is a benchmark thing, not sure, so if you comment out(#) the
print 'no match' function in line 7, it will run quicker.

end_point increments as a boolean, to match the high_num. When that
happens, an if occurs, the match is printed, and the function returns to
the instance caller.

Use the other posters suggestions if they are better suited for you,
however, I'd read what those functions do to give a more experienced lesson
in how the 'experts' would code it out.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dave Angel
On 09/01/2012 01:46 AM, Dwight Hutto wrote:
> Here's the better function fixed with a return statement I forgot, but
> might not be as quick as the pre-built functions already shown:
>
> def iterToHighNum(increment,high_num):
> end_point = 0
> a = [i for i in range(0,increment)]
> while (end_point != high_num) == True:
> for integer in a:
> if integer != high_num:
> print "no match, integer = %i" % (integer)
> end_point += 1
> if end_point == high_num and integer != (high_num - 1):
> print 'match, integer = %i, high_num = %i' %
> (integer,high_num)
> return
>
> previous_increment = increment
> increment += increment
> a = [i for i in range(previous_increment,increment)]
>
> #instance
> increment = 1
> high_num = 100
> iterToHighNum(increment,high_num)
>
>
I'm not sure what the point of any of that is;  you're making a simple
problem complex.  If you're wanting to accomplish the task without using
any of the itertools stuff, why not just:


current = 10**9
lim = 10**10
while current < lim:
 print current   #or write to file, or whatever
 current += 1


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
Here's the better function fixed with a return statement I forgot, but
might not be as quick as the pre-built functions already shown:

def iterToHighNum(increment,high_num):
end_point = 0
a = [i for i in range(0,increment)]
while (end_point != high_num) == True:
for integer in a:
if integer != high_num:
print "no match, integer = %i" % (integer)
end_point += 1
if end_point == high_num and integer != (high_num - 1):
print 'match, integer = %i, high_num = %i' %
(integer,high_num)
return

previous_increment = increment
increment += increment
a = [i for i in range(previous_increment,increment)]

#instance
increment = 1
high_num = 100
iterToHighNum(increment,high_num)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
It might be a little buggy, but I'm in a rush, so it has a flaw in it. But
I think you get the point I'm trying to make.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
I mean line 7.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
You can also watch this happen by uncommenting print on line 8:


def iterToHighNum(increment,high_
num):
end_point = 0
a = [i for i in range(0,increment)]
while (end_point != high_num) == True:
for integer in a:
if integer != high_num:
print "no match, integer = %i" % (integer)
end_point += 1
if end_point == high_num and integer != (high_num - 1):
print 'match, integer = %i, high_num = %i' %
(integer,high_num)

previous_increment = increment
increment += increment
a = [i for i in range(previous_increment,increment)]

#instance
increment = 1
high_num = 100
iterToHighNum(increment,high_num)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread eryksun
On Sat, Sep 1, 2012 at 12:29 AM, Scurvy Scott  wrote:
>
> The while loop works for simply printing. Now I'm trying to save to a file
>
> I = 10
> Boogers = raw_input("file")
> Baseball = open(Boogers)

As ASCII, that's 11 bytes per number times 9 billion numbers. That's
approximately 92 GiB (about 21 DVD-5 discs). Are you sure you want to
do that?

Minor correction:

>>counter = count(start=10**9L)
>>slices = (islice(counter, 10**9) for i in range(10))
>>nums = chain.from_iterable(slices)

That should have been range(9), not range(10).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dwight Hutto
Dear Scurvy,
I don't know if this has been suggested yet, but I just broke the
larger list of 10 digit nums into segments, and at the end of the loop,
kept the previous last num in the segment as the beginning of the next
range() that goes through the same amount of ints in each segmentation of
the larger list to iterate through.

Except the next list of ints is starting where the last set segmentation of
possible length int segments ends, and keeps iterating through all possible
nums until it hits a match high_num.

The following goes through 10,000 element lists until it hits a target
high_num, which in this case is 1,000,000.



def iterToHighNum(increment,high_num):
end_point = 0
a = [i for i in range(0,increment)]
while (end_point != high_num) == True:
for integer in a:
if integer != high_num:
#print "no match, integer = %i" % (integer)
end_point += 1
if end_point == high_num and integer != (high_num - 1):
print 'match, integer = %i, high_num = %i' %
(integer,high_num)

previous_increment = increment
increment += increment
a = [i for i in range(previous_increment,increment)]

#instance
increment = 1
high_num = 100
iterToHighNum(increment,high_num)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dave Angel
On 08/31/2012 10:38 PM, Scurvy Scott wrote:
> Now I've got
> 
> A = 10
> I = raw_input("file name> ")
> TheFile = open(I, 'w')
> 
> TheFile.truncate
> def allten(a):
> while a < 99:
> a =  + 1
> TheFile.write(a)
> allten(a)
> 
> The result is:
> Open file 'file.txt', mode w at 0xb7331ee8
> Traceback line 13 etc
> 

Top-posted again.

You stop the traceback before showing anything interesting.  i don't see
13 lines, so I have to totally guess which line is getting the error,
and also what error it's getting.  Paste the code and the traceback
directly, and if that means you have to get email working on whatever
machine you're running Python on, then do so.  Retyping on your iphone
isn't productive for anyone.

There are a pile of typos in that code.  Are they just because you
retyped, or is that really what you tried?

1) TheFile.truncate doesn't call anything.  The line is useless without
parentheses.  Of course, that doesn't matter much, since the open
already truncated it.

2)  allien(a)There's no such variable as 'a' defined.  you do have
one called A

3) write() takes a string.  You're passing it an int or long.  Convert
with the str function, or other method, depending on what you really want.

4) Once you fix #2 and #3, the first value you write will be 1001
That's off by one.  As i said in my last message, you should swap the
two lines.  And once you do, you'll end one value too low.  As I also
said there, you should test the loop with smaller numbers.



-- 

DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dave Angel
On 08/31/2012 10:14 PM, Scurvy Scott wrote:
> Thanks for the reply. This isn't an assignment per se as I'm just learning 
> python for my own sake- not in classes or school or what have you. As I said 
> I'm pretty new to python picking up whatever I can and generators are 
> something that I haven't grasped. They're functions(kinda) that in some way 
> deal with lists, tuples, or dict,(kinda?). I am probably wrong on this. Some 
> clarification would be excellent. 
> 
> Thank you in advance and I apologize for being not so sharp sometimes.
> 
> 

You seem plenty sharp enough to me.  I do have to point out a couple of
list etiquette points:

1) You top-posted.  That means you put your message BEFORE the part
you're quoting.  Once a few replies go back and forth, this thoroughly
scrambles the order, so you might as well delete all the context.

2) You replied privately to me.  I've been participating in public
forums like this for over 20 years, and the key word is "public." The
only messages to send privately are thank-yous, and ones with personal
information in them, such as passwords and such.  Instead you should
reply-all, and if your email is too broken to offer that, add the cc of
tutor@python.org

A simple generator could be

def bigrange(start, end):
i = start
while i < end:
yield i
i += 1

The yield is a little like a return, in that the "caller" gets the
value.  But the function stack is kept active, and next time the loop
needs a value, it resumes the same function.  Control gets batted back
and forth between the loop code and the generator code, until the
generator finally returns.  In this case it returns when it reaches the
end value.

So it'd be used like:

for x in bigrange(100, 1000):
print x

I do hope you've been testing with smaller numbers than 10**10, to make
sure the loops you write really do start and end with reasonable results.


> I = 10
> While I < 99:
> Print I

> Is that more like it?
-- 

Did you try running it?  You never increment I, so it'll repeat forever
on the one value.

> I meant;
>
> I = 10
> While I < 99:
> I += 1
> Print I
>
>
> But that doesn't work.

Want to explain what about it doesn't work?  That phrase could mean that
you got an error (post traceback), or it ran forever, or it displayed
roman numerals.

  The only problem I see is it starts one-too-high.  Fix that by
swapping the last two lines.

> Apologies for not mentioning I'm on 2.x.x I've seen so much about
> avoiding 3.x I just thought it was assumed.

And I've seen so much about avoiding 2.x that i figured 3.x would be
assumed.  Best to be explicit:   python version, operating system, etc.


DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 9:08 PM, Scurvy Scott  wrote:
>
> My question is this- I've been trying for a month to generate a list of
> all possible 10 digit numbers. I've googled, looked on stackoverflow,
> experimented with itertools,

In itertools, look at count() and islice(). An alternative to islice
in this case is takewhile().

count() works with Python long integers. For example, counter =
count(start=10**9L).

islice() is limited to sys.maxint (sys.maxsize in Python 3), so you
need to chain several together on a 32-bit platform (also 64-bit
Windows, I gather, since a C long is always 32-bit on Windows). Use a
generator expression with chain.from_iterable:

counter = count(start=10**9L)
slices = (islice(counter, 10**9) for i in range(10))
nums = chain.from_iterable(slices)

Since printing performance is limited by the terminal's speed, you
probably don't need the efficiency of islice (with its machine word
size limit). Instead, you can use takewhile() with a pure Python
predicate. For example:

counter = count(start=10**9L)
nums = takewhile(lambda x: x < 10**10, counter)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] List all possible 10digit number

2012-08-31 Thread Dave Angel
On 08/31/2012 09:08 PM, Scurvy Scott wrote:
> First of all thank you guys for all your help. The manual is really no 
> substitute for having things explained in laymans terms as opposed to a 
> technical manual.
>
> My question is this- I've been trying for a month to generate a list of all 
> possible 10 digit numbers. I've googled, looked on stackoverflow, 
> experimented with itertools, lists, etc to no avail. The closest I've gotten 
> is using itertools to generate every possible arrangement of a list
>
> List = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>
> I feel like I'm close but can't quite get it. Any suggestions or shoves in 
> the right direction would be helpful. An issue I've encountered is that 
> python won't do something like
>
> For I in range(10, 99):
> Print I
>
> Without crashing or throwing an exception.
>
> I've also tried to do something like
>
> I in range (100, 900):
>Etc
> And then concatenate the results but also to no avail.
>
> Again, any shoves in the right direction would be greatly appreciated.
>

You should have mentioned that you're using Python 2.x.  (Things are
different in 3.x )

The range() function is the wrong answer, since 9 billion integers
aren't going to fit in memory, on most machines.   You should at least
have tried xrange().  Unfortunately, it's limited to ints, which only go
to a couple billion.  That's probably why your assignment specifies 10
digits.

The easy answer is to do a while loop that's equivalent to the xrange().
Initialize your counter to the smallest value you're interested in, and
each time through the loop, print the counter and increment it.  The
loop should terminate when you pass the highest value of interest.

The "right" answer is to write a generator, using yield to yield each
value.  Interestingly, it's the same loop as previous, but it yields
instead of prints.
Then, once you have the generator, you write a loop using it, rather
than xrange.


-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 102, Issue 98

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 8:20 PM, Steven D'Aprano  wrote:
>
> Sequence
>   The generalisation of lists, tuples and strings. Any object that has
>   a known length where individual items can be retrieved with the
>   __getitem__ method called sequentially: obj[0], obj[1], obj[2], ...

To expand on this, any object that has a __getitem__ method can be
iterated over until it raises an IndexError, not just object's that
have an __iter__ method, and __len__ is not a factor. For example:

class Test(object):
def __getitem__(self, n):
if n > 4:
raise IndexError
return 'test'

>>> list(Test())
['test', 'test', 'test', 'test', 'test']
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] List all possible 10digit number

2012-08-31 Thread Scurvy Scott
First of all thank you guys for all your help. The manual is really no 
substitute for having things explained in laymans terms as opposed to a 
technical manual.

My question is this- I've been trying for a month to generate a list of all 
possible 10 digit numbers. I've googled, looked on stackoverflow, experimented 
with itertools, lists, etc to no avail. The closest I've gotten is using 
itertools to generate every possible arrangement of a list

List = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I feel like I'm close but can't quite get it. Any suggestions or shoves in the 
right direction would be helpful. An issue I've encountered is that python 
won't do something like

For I in range(10, 99):
Print I

Without crashing or throwing an exception.

I've also tried to do something like

I in range (100, 900):
   Etc
And then concatenate the results but also to no avail.

Again, any shoves in the right direction would be greatly appreciated.

Scott
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using a calling program to change Python script arguments

2012-08-31 Thread Ray Jones
On 08/31/2012 04:58 PM, Alan Gauld wrote:
>
> Creating a module is just a matter of creating a standard python file
>
> 
> #! /bin/python# you don't even really need a shebang for modules!
> myVar = 66
> < end of myvar.py -->
>
> import myvar
> print myvar.myVal
>
>
> And so long as the location of the module is in your sys.path
> (or in the PYHONPATH environment variable) python will find it.

Yep. I got my 'pymodules' directory created, and I will append it to the
sys.path list a run time. So far it appears to work in testing
modenext we'll see what happens in real life!

Thanks.


Ray
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 102, Issue 98

2012-08-31 Thread Steven D'Aprano

On 31/08/12 18:31, Mark Lawrence wrote:

On 31/08/2012 04:27, William R. Wing (Bill Wing) wrote:


How about -


for item in iter(list):
….print item


Overengineering? :) A list is an iterator.



Technically, no it isn't, it is an "iterable" or a "sequence" but
not an iterator.

py> mylist = [1, 2, 3]
py> next(mylist)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: list object is not an iterator



You are right to question the call to iter, it is redundant in this
case, but your terminology is mixed up.

There are three terms normally used to describe things that can
be used in for-loops:

Sequence
  The generalisation of lists, tuples and strings. Any object that has
  a known length where individual items can be retrieved with the
  __getitem__ method called sequentially: obj[0], obj[1], obj[2], ...

Iterator
  Any object that obeys the "iterator protocol", that is, it must have
  a method __iter__ which returns itself, and a method __next__ which
  returns the iterator items one at a time, then raises StopIteration
  when there are no more items. Examples of iterators include generator
  expressions, generators, the functions from the itertools module,
  and in Python 3, built-ins map, filter and zip.

Iterable
  Any object which can be iterated over, that is, a sequence or
  iterator.


The iter() built-in calls the object's __iter__ method. If the object
is already an iterator, it returns itself unchanged. Since lists are
not iterators, iter(list) returns a new iterator object:

py> it = iter(mylist)
py> it

py> iter(it) is it
True



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using a calling program to change Python script arguments

2012-08-31 Thread Alan Gauld

On 31/08/12 22:51, Ray Jones wrote:



Okay. Now I must figure out how to create the module and have my calling
script look in the right place ;)


Creating a module is just a matter of creating a standard python file


#! /bin/python# you don't even really need a shebang for modules!
myVar = 66
< end of myvar.py -->

import myvar
print myvar.myVal


And so long as the location of the module is in your sys.path
(or in the PYHONPATH environment variable) python will find it.



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power of Two Function

2012-08-31 Thread Alan Gauld

On 31/08/12 23:54, Lazar wrote:


Can someone please explain to me in what way the following function
checks if a number is a power of two? Basically, the second line of
code is what I can't really grasp:

def is_power2(num):
return num != 0 and ((num & (num - 1)) == 0)



In binary any positive power of 2 looks like 1 followed by zeros:
10, 100, 1000 etc

If you subtract 1 from  any of those numbers you get
a zero followed by all 1s:

01, 011, 0111 etc

So if you bitwise and N and N-1 you get

  10
& 01
--
  00

ie all zeros which equals the decimal number zero.


So your function first checks that the argument is not zero
if that is true then it evaluates the second part which as
we've seen does equal zero for a power of two.

Unfortunately it breaks down for fractions like 1/2, 1/4 etc
which are negative powers of 2.

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem caused by installing 2.7.3

2012-08-31 Thread Mark Lawrence

On 01/09/2012 00:16, Richard D. Moores wrote:

On Fri, Aug 31, 2012 at 10:57 AM, eryksun  wrote:

On Fri, Aug 31, 2012 at 12:49 PM, Richard D. Moores  wrote:



https://bitbucket.org/vinay.sajip/pylauncher



https://bitbucket.org/vinay.sajip/pylauncher/raw/tip/Doc/launcher.rst


Thank you! You solved my problem.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



For the record this is described in PEP 397 and forms part of the Python 
3.3 release.


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power of Two Function

2012-08-31 Thread Lazar
Visar and Bob,

Thank you for your detailed explanations, I appreciate your help.

Kind regards,
Lazar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power of Two Function

2012-08-31 Thread bob gailer

On 8/31/2012 6:54 PM, Lazar wrote:

Hello,

I'm fairly new to Python and still learning.

Can someone please explain to me in what way the following function
checks if a number is a power of two? Basically, the second line of
code is what I can't really grasp:

def is_power2(num):
return num != 0 and ((num & (num - 1)) == 0)
A power of 2 in binary is (in general) one followed by zero-or-more 
zeroes. Thus

2 = 10
4 = 100
8 = 1000

The python manual informs us


 5.4.1. Bit-string Operations on Integer Types

Plain and long integer types support additional operations that make 
sense only for bit-strings. Negative numbers are treated as their 2's 
complement value (for long integers, this assumes a sufficiently large 
number of bits that no overflow occurs during the operation).



x & y   bitwise /and/ of /x/ and /y/


in other words & takes two bit strings, does a boolean and on pairs of 
bits. Thus

1100 & 1010 = 1000 (you get 1 only where the two bits are 1.

for any /integer /n that is a power of 2, n-1 will be a string of all 
ones length one less than n. Thus given n = 4 (100), n-1 is 11. Attach a 
leading 0, perform the & and the result will be 0. Thus


num & (num - 1)) == 0 will be one only for powers of 2.

The function returns 1 or  0 which may be interpreted as True or False by the 
caller.

Why the function does not ensure that its argument is of type int is a problem.

HTH.

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power of Two Function

2012-08-31 Thread Visar Zejnullahu
http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
You have many useful bit hacks here.

Visar Zejnullahu


On Sat, Sep 1, 2012 at 1:17 AM, Visar Zejnullahu  wrote:

> 2^n in binary is 10...0 (with n 0s), and 2^n - 1 is 11...1 (with n 1s). So
> if you do bitwise and (&) to 2^n and 2^n-1 you get all 0s. That's why you
> check if (num & (num - 1)) == 0.
>
> Visar Zejnullahu
>
>
>
> On Sat, Sep 1, 2012 at 12:54 AM, Lazar  wrote:
>
>> Hello,
>>
>> I'm fairly new to Python and still learning.
>>
>> Can someone please explain to me in what way the following function
>> checks if a number is a power of two? Basically, the second line of
>> code is what I can't really grasp:
>>
>> def is_power2(num):
>> return num != 0 and ((num & (num - 1)) == 0)
>>
>> Thank you,
>> Lazar
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power of Two Function

2012-08-31 Thread Visar Zejnullahu
2^n in binary is 10...0 (with n 0s), and 2^n - 1 is 11...1 (with n 1s). So
if you do bitwise and (&) to 2^n and 2^n-1 you get all 0s. That's why you
check if (num & (num - 1)) == 0.

Visar Zejnullahu


On Sat, Sep 1, 2012 at 12:54 AM, Lazar  wrote:

> Hello,
>
> I'm fairly new to Python and still learning.
>
> Can someone please explain to me in what way the following function
> checks if a number is a power of two? Basically, the second line of
> code is what I can't really grasp:
>
> def is_power2(num):
> return num != 0 and ((num & (num - 1)) == 0)
>
> Thank you,
> Lazar
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem caused by installing 2.7.3

2012-08-31 Thread Richard D. Moores
On Fri, Aug 31, 2012 at 10:57 AM, eryksun  wrote:
> On Fri, Aug 31, 2012 at 12:49 PM, Richard D. Moores  
> wrote:

> https://bitbucket.org/vinay.sajip/pylauncher

> https://bitbucket.org/vinay.sajip/pylauncher/raw/tip/Doc/launcher.rst

Thank you! You solved my problem.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Power of Two Function

2012-08-31 Thread Lazar
Hello,

I'm fairly new to Python and still learning.

Can someone please explain to me in what way the following function
checks if a number is a power of two? Basically, the second line of
code is what I can't really grasp:

def is_power2(num):
return num != 0 and ((num & (num - 1)) == 0)

Thank you,
Lazar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using a calling program to change Python script arguments

2012-08-31 Thread Ray Jones
On 08/31/2012 02:19 PM, Alan Gauld wrote:
> On 31/08/12 18:05, Ray Jones wrote:
>
>> script and have it parse. Is there another method for one Python script
>> to call/import/execute a Python script and integrate the name space so
>> that the variables in each of the calling scripts would be directly
>> usable by the Python module/child process/whatever without passing
>> arguments either via line arguments (yeccchhh!) or function calls
>
>
> Just create a module with the variables in it.
>
> Then import and access that module data in all your scripts.
>
> eg.
>
> import mysitedata
> print mysitedata.myValue
Backasswards was I again. I was thinking of loading the main from my
calling program.

I will try that. The only thing I would have to pass on the command line
then is which module I want to import for the site I want to access.

Okay. Now I must figure out how to create the module and have my calling
script look in the right place ;)

Thanks.


Ray
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using a calling program to change Python script arguments

2012-08-31 Thread Alan Gauld

On 31/08/12 18:05, Ray Jones wrote:


script and have it parse. Is there another method for one Python script
to call/import/execute a Python script and integrate the name space so
that the variables in each of the calling scripts would be directly
usable by the Python module/child process/whatever without passing
arguments either via line arguments (yeccchhh!) or function calls



Just create a module with the variables in it.

Then import and access that module data in all your scripts.

eg.

import mysitedata
print mysitedata.myValue

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem caused by installing 2.7.3

2012-08-31 Thread eryksun
On Fri, Aug 31, 2012 at 12:49 PM, Richard D. Moores  wrote:
>
> MS Windows 7 Home Premium 64-bit SP1
>
> I have some useful (to me) scripts that I use frequently, that I call
> with Windows shortcut keys. They used to open in a nice cmd.exe window
> I'd configured to my liking. Now I find that they open in the default
> white on black tiny window, C:\Python27\python.exe, which tries to run
> the 3.x scripts in 2.7. How did that change come about, and how can I
> correct it?

.py files are associated (assoc .py) with Python.File (ftype
Python.File), which defines the command to run .py console scripts.
The 2.x installer overwrote the registry keys. If you plan to write
scripts for both 2.x and 3.x, the best solution, IMO, is to install
pylauncher to add shebang support to your scripts:

https://bitbucket.org/vinay.sajip/pylauncher

The simpler of the two installations for you would be
launchwin.amd64.msi. It puts py.exe and pyw.exe in the Windows folder,
so you don't have to muck with PATH. Once installed your scripts will
run with py.exe, which takes care of parsing the shebang and starting
the required interpreter.

You can define additional shebangs in %LOCALAPPDATA%\py.ini (e.g. to
add support for pypy). I think this also works in %APPDATA%\py.ini if
you'd rather use your roaming profile. See the docs for more
information:

https://bitbucket.org/vinay.sajip/pylauncher/raw/tip/Doc/launcher.rst
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using a calling program to change Python script arguments

2012-08-31 Thread Ray Jones
As an aid to learning Python, I am currently in the process of
converting my Bash scripts into Python scripts. Through the years, as I
have accumulated a variety of sites, I have been maintaining a half
dozen or so Bash scripts that basically do the same thing: log me into a
streaming video site and record the stream.

So as I make this conversion, it's becoming obvious to me that as my
Python skill increases, I will be making changes to these scripts. But
that means maintaining all these scripts in parallel with each other.

I'm considering creating calling scripts (either Bash or another Python
script), all of which call a single Python capture script. Each calling
script would have its own info concerning IP, port, user name, password,
http or rtsp, time zone info, length of capture time, etc., and then
those scripts would pass their information to the single Python script,
greatly easing the maintenance issue.

But that's a ton of command line arguments to pass to the Python capture
script and have it parse. Is there another method for one Python script
to call/import/execute a Python script and integrate the name space so
that the variables in each of the calling scripts would be directly
usable by the Python module/child process/whatever without passing
arguments either via line arguments (yeccchhh!) or function calls
(considerably better)? Arguably, once the calling script passed the info
to the main script, it really wouldn't need to be there at all.

So how high is this pie-in-the-sky dream of mine?


Ray
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem caused by installing 2.7.3

2012-08-31 Thread Richard D. Moores
MS Windows 7 Home Premium 64-bit SP1

I've been using 3.x for a long time, but the other day I thought it
would also be good to have the latest version of 2.x available. So I
downloaded it and installed it.

I have some useful (to me) scripts that I use frequently, that I call
with Windows shortcut keys. They used to open in a nice cmd.exe window
I'd configured to my liking. Now I find that they open in the default
white on black tiny window, C:\Python27\python.exe, which tries to run
the 3.x scripts in 2.7. How did that change come about, and how can I
correct it?

FWIW, the path in System Variables is

C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program
Files (x86)\Common Files\Microsoft Shared\Windows
Live;C:\Python32\;C:\Python31\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program
Files (x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL
Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL
Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL
Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET
Web Pages\v1.0\;C:\Program Files\MySQL\MySQL Server 5.1\bin;C:\Program
Files (x86)\QuickTime\QTSystem\

Thanks,

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 102, Issue 98

2012-08-31 Thread William R. Wing (Bill Wing)
On Aug 31, 2012, at 4:31 AM, Mark Lawrence  wrote:

> On 31/08/2012 04:27, William R. Wing (Bill Wing) wrote:
>> 
>> How about -
>> 
> for item in iter(list):
> ….print item
> 
> Overengineering? :) A list is an iterator.
> 

Right you are - should have been:

for item in list:
print item

-Bill

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using multiprocessing efficiently to process large data file

2012-08-31 Thread Prasad, Ramit
Please always respond to the list. And avoid top posting.

> -Original Message-
> From: Abhishek Pratap [mailto:abhishek@gmail.com]
> Sent: Thursday, August 30, 2012 5:47 PM
> To: Prasad, Ramit
> Subject: Re: [Tutor] using multiprocessing efficiently to process large data
> file
> 
> Hi Ramit
> 
> Thanks for your quick reply. Unfortunately given the size of the file
> I  cant afford to load it all into memory at one go.
> I could read, lets say first 1 million lines process them in parallel
> and so on. I am looking for some example which does something similar.
> 
> -Abhi
> 

The same logic should work just process your batch after checking size
and iterate over the file directly instead of reading in memory.

with open( file, 'r' ) as f:
iterdata = iter(f)
grouped_data =[]
for d in iterdata:
l = [d, next(iterdata)] # make this list 8 elements instead
grouped_data.append( l )
if len(grouped_data) > 100/8: # one million lines
# process batch
grouped_data = []


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Mark Lawrence

On 31/08/2012 12:51, Steven D'Aprano wrote:

On 31/08/12 19:13, Mark Lawrence wrote:
[...]

Please don't top post.


And Mark, please trim your replies. Bottom posting without trimming is
just as annoying as top posting without trimming.

(Among other things, your post ended up containing THREE copies of the
mailing list footer.)



My apologies but I'd swear blind that I'd done just that.  Who's been 
playing with the time machine? :)


--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Steven D'Aprano

On 31/08/12 19:13, Mark Lawrence wrote:
[...]

Please don't top post.


And Mark, please trim your replies. Bottom posting without trimming is
just as annoying as top posting without trimming.

(Among other things, your post ended up containing THREE copies of the
mailing list footer.)



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Mark Lawrence

On 31/08/2012 09:44, Kal Sze wrote:

That's left as an exercise to the reader.

On Friday, August 31, 2012, Mark Lawrence wrote:


On 31/08/2012 08:55, Alan Gauld wrote:



Now just translate that into Scheme :-)

HTH



Anyone know of an application to automate Python to Scheme translation? :)

--
Cheers.

Mark Lawrence.

__**_
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/**mailman/listinfo/tutor





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



Please don't top post.

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] checking input parameters

2012-08-31 Thread Peter Otten
Bala subramanian wrote:


> I use the following way to check for the input parameters. I would
> like to know if there is a any better way to show or describe the
> script usage. So when the user just runs it without any input params.,
> the program shd not execute but just shows the documentation.
> 
> from sys import argv
> if not argv[1:]:
>   print 'Program usage: python script file-list1 file-list2'
>   print 'file list should contain the names of the atom list'
>   
>   

I may look like a lot of overhead at first, but in the long run it will pay 
off if you use argparse:

$ cat argparse_demo.py

def main():
import argparse
parser = argparse.ArgumentParser(
description="what the script is supposed to do")
parser.add_argument(
"file_list1", metavar="file-list1",
help="your help text for file-list1")
parser.add_argument(
"file_list2", metavar="file-list2",
help="your help text for file-list2")
args = parser.parse_args()

print "Do something with {!r} and {!r}".format(
args.file_list1, args.file_list2)

if __name__ == "__main__":
main()
$ python argparse_demo.py 
usage: argparse_demo.py [-h] file-list1 file-list2
argparse_demo.py: error: too few arguments
$ python argparse_demo.py -h
usage: argparse_demo.py [-h] file-list1 file-list2

what the script is supposed to do

positional arguments:
  file-list1  your help text for file-list1
  file-list2  your help text for file-list2

optional arguments:
  -h, --help  show this help message and exit
$ python argparse_demo.py alpha.txt beta.txt
Do something with 'alpha.txt' and 'beta.txt'


For more see the documentation at 
http://docs.python.org/howto/argparse.html#id1

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Kal Sze
That's left as an exercise to the reader.

On Friday, August 31, 2012, Mark Lawrence wrote:

> On 31/08/2012 08:55, Alan Gauld wrote:
>
>>
>> Now just translate that into Scheme :-)
>>
>> HTH
>>
>>
> Anyone know of an application to automate Python to Scheme translation? :)
>
> --
> Cheers.
>
> Mark Lawrence.
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Mark Lawrence

On 31/08/2012 08:55, Alan Gauld wrote:


Now just translate that into Scheme :-)

HTH



Anyone know of an application to automate Python to Scheme translation? :)

--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] checking input parameters

2012-08-31 Thread Bala subramanian
Friends,
I use the following way to check for the input parameters. I would
like to know if there is a any better way to show or describe the
script usage. So when the user just runs it without any input params.,
the program shd not execute but just shows the documentation.

from sys import argv
if not argv[1:]:
  print 'Program usage: python script file-list1 file-list2'
  print 'file list should contain the names of the atom list'
  
  
Thanks,
Bala

-- 
C. Balasubramanian
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 102, Issue 98

2012-08-31 Thread Mark Lawrence

On 31/08/2012 04:27, William R. Wing (Bill Wing) wrote:


How about -


for item in iter(list):
….print item


Overengineering? :) A list is an iterator.



-Bill
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




--
Cheers.

Mark Lawrence.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing list in a Column

2012-08-31 Thread Alan Gauld

On 31/08/12 02:12, Ashley Fowler wrote:

Can anyone help me edit this code below to return the list in the form
of a column instead of a row?

def printList():
 list1 = input("Insert a list")
 list = [list1]
 print (list)


First you need to convert the string that your user types into a list of 
individual entries. At the moment your list consists of a single string 
so the column will only have a single line.


You probably want to look at the split() method of strings.

Alternatively you could write a loop which reads each entry individually 
from the user.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scheme

2012-08-31 Thread Alan Gauld

On 31/08/12 00:32, Ashley Fowler wrote:

This is a problem using the Scheme programming...Can anybody help me
with this problem?

2. Write a procedure (sphere r) that takes the radius of a sphere
as the value of its input parameter and returns the volume of that
sphere given by the formula: (4/3)π(r^3). Use (require scheme/math)
or (require racket/math) to load the math library containing the
"pi" constant.


from math import pi
def sphere(r):
return (4.0/3) * math.pi *(r**3)


Be sure to use "cube" from problem (1) to find the cube of r (r^3).


Sorry I don't have access to problem 1 but luckily in Python I don't 
need it!


Now just translate that into Scheme :-)

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lambda?? Whaaaaat?

2012-08-31 Thread Alan Gauld

On 31/08/12 05:43, Steven D'Aprano wrote:


Where does the name come from? Lambda is the Greek letter L,
and for reasons I don't know, it is the traditional name used
for functions in some of the more abstract areas of computer
science.


More specifically it is the name of a branch of mathematics called 
Lambda Calculus which is the theoretical underpinning of a lot of 
computer science. It predates the study of computing (in the modern 
mechanised sense) in the same way as Boolean logic predates modern 
computing, but both are essential theoretical building blocks.


Python's lambda feature is therefore useful in formal teaching 
scenarios, which was an important aspect of Python as originally 
perceived. Python grew out of several other teaching languages and a lot 
of the early features were explicitly added to assist in teaching 
computing theory to students.


Google Lambda Calculus for more but be prepared to have your mind 
boggled. It's not an intuitive branch of math!


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor