Re: [Tutor] (no subject)

2012-09-15 Thread Steven D'Aprano

On 15/09/12 15:09, Johny Rei wrote:


hi  to all readers, i 'm a newbie and i'm interested to learn
python programming, can anybody please guide me out to learn
basic to advance python programming, be actually can anyone out
there should suggest a free book that i can read it on just to
learn from as a newbie to python programming? i can be kindly
respect and appreciate the guidance you can  recommend it to
me..thnx



Try this one:

http://www.springer.com/mathematics/computational+science+%26+engineering/book/978-3-642-30292-3

http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf



--
Steven

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


Re: [Tutor] (no subject)

2012-09-15 Thread Albert-Jan Roskam
___

> From: Johny Rei 
>To: "tutor@python.org"  
>Sent: Saturday, September 15, 2012 7:09 AM
>Subject: [Tutor] (no subject)
> 
>
>hi  to all readers, i 'm a newbie and i'm interested to learn python 
>programming, can anybody please guide me out to learn basic to advance python 
>programming, be actually can anyone out there should suggest a free book that 
>i can read it on just to learn from as a newbie to python programming? i can 
>be kindly respect and appreciate the guidance you can  recommend it to me..thnx
>

I like this one a LOT: http://www.qtrac.eu/py3book.html. It's really an art how 
well the author can explain complex things. It's about Python 3, but that 
hardly matters. Even though I've read just about everything of it now, I 
regularly pick it up again and discover new stuff. Some chapters are a bit over 
my head (e.g. one about metaclasses).


Albert-Jan

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


Re: [Tutor] is this use or abuse of __getitem__ ?

2012-09-15 Thread eryksun
On Fri, Sep 14, 2012 at 2:33 PM, Albert-Jan Roskam  wrote:
>> On 14/09/12 22:16, Albert-Jan Roskam wrote:
>
> Is it recommended to define the geitem() function inside the __getitem__() 
> method?
> I was thinking I could also define a _getitem() private method.
>
> def getitem(key):
> retcode1 = self.iomodule.SeekNextCase(self.fh, 
> ctypes.c_long(int(key)))
> 


I wouldn't do this since it incurs the cost of a repeated function
call. A slice could involve thousands of such calls. Maybe use a
boolean variable like "is_slice". Then use a for loop to build the
records list (maybe only 1 item). If is_slice, return records, else
return records[0].


> if isinstance(key, slice):
> records = [getitem(i) for i in range(*key.indices(self.nCases))]
> return records
> elif hasattr(key, "__int__"): # isinstance(key, (int, float)):
> if abs(key) > (self.nCases - 1):
> raise IndexError
> else:
> key = self.nCases + key if key < 0 else key
> record = getitem(key)
> return record
> else:
> raise TypeError


I agree with Steven's reasoning that it doesn't make sense to support
floating point indexes. Python 2.6+ has the __index__ special method.
int and long have this method. float, Decimal,and Fraction do not have
it. It lets you support any user-defined class that can be used as an
index. For example:

>>> class MyInt(object):
... def __index__(self):
... return 5

>>> slice(MyInt(), MyInt(), MyInt()).indices(10)
(5, 5, 5)

operator.index() is the corresponding function. It raises TypeError if
__index__ isn't supported.

But watch out because you're using ctypes.c_long. It doesn't do any
range checking. It just silently wraps around modulo the size of a
long on your platform:

>>> c_long(2**32-1), c_long(2**32), c_long(2**32+1)
(c_long(-1), c_long(0), c_long(1))

Calling int(key) or index(key) is no help because it will silently
return a Python long (big int). You need to do range checking on the
upper bound and raise a ValueError.

For example:

from operator import index  # calls obj.__index__()

is_slice = isinstance(key, slice)

if is_slice:
start, stop, step = key.indices(self.nCases)  # may raise TypeError
else:
start = index(self.nCases + key if key < 0 else key)  # may
raise TypeError
stop = start + 1
step = 1

if stop > 2 ** (ctypes.sizeof(ctypes.c_long) * 8 - 1):
raise ValueError('useful message')

records = []
for i in range(start, stop, step):
retcode1 = self.iomodule.SeekNextCase(self.fh, ctypes.c_long(i))
self.caseBuffer, self.caseBufferPtr = self.getCaseBuffer()
retcode2 = self.iomodule.WholeCaseIn(self.fh, self.caseBufferPtr)
record = struct.unpack(self.structFmt, self.caseBuffer.raw)
if any([retcode1, retcode2]):
raise RuntimeError("Error retrieving record %d [%s, %s]" %
(i, retcodes[retcode1], retcodes[retcode2]))
records.append(record)

if not is_slice:
records = records[0]
return records
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2012-09-15 Thread Dwight Hutto
How to think like a computer scientist, in python:

http://greenteapress.com/thinkpython/thinkpython.html

And plenty of practice:

Print
Lists
Dicts
Tuples
DB Files, you parse for data yourself

Basic manipulation of data.


-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is this use or abuse of __getitem__ ?

2012-09-15 Thread eryksun
On Sat, Sep 15, 2012 at 4:43 AM, eryksun  wrote:

> else:
> start = index(self.nCases + key if key < 0 else key)  # may
> raise TypeError
> stop = start + 1
> step = 1


Gmail is such a pain sometimes. I should have called index first anyway:

key = index(key)  # may raise TypeError
start = key + self.nCases if key < 0 else key
stop = start + 1
step = 1


> records = []
> for i in range(start, stop, step):
> ...
> records.append(record)


You can boost the performance here a bit by caching the append method.
This avoids a LOAD_ATTR operation on each iteration:

records = []
append = records.append
for i in range(start, stop, step):
...
append(record)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [Semi-OT] Yes or no on using a Graphical IDE?

2012-09-15 Thread leam hall
Hey all, not trying to contribute to the flames of one graphical IDE over
another. I'm just trying to figure out if they are worth the learning
curve? I have been doing most of my work in vi and the graphical IDE I'm
supposed to use for a class keeps adding crap that I have to erase, and I
have to move my hands to use the GUI parts instead of just typing.

Is there a point in which a GUI IDE becomes more of a help than a hindrance?

Thanks!

Leam

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


Re: [Tutor] is this use or abuse of __getitem__ ?

2012-09-15 Thread Albert-Jan Roskam
>On Sat, Sep 15, 2012 at 4:43 AM, eryksun  wrote:
>
>>     else:
>>         start = index(self.nCases + key if key < 0 else key)  # may
>> raise TypeError
>>         stop = start + 1
>>         step = 1
>
>
>Gmail is such a pain sometimes. I should have called index first anyway:

>

>        key = index(key)  # may raise TypeError
>        start = key + self.nCases if key < 0 else key
>        stop = start + 1
>        step = 1
>

Thanks, I hadn't noticed this yet. I am refactoring some of the rest of my code 
and I hadn't run anything yet. My code has two methods that return record(s): 
an iterator (__getitem__) and a generator (readFile, which is also called by 
__enter__). Shouldn't I also take the possibility of a MemoryError into account 
when the caller does something like data[:10**8]? It may no longer fit into 
memory, esp. when the dataset is also wide.


>
>>     records = []
>>     for i in range(start, stop, step):
>>         ...
>>         records.append(record)
>
>
>You can boost the performance here a bit by caching the append method.
>This avoids a LOAD_ATTR operation on each iteration:
>
>    records = []
>    append = records.append
>    for i in range(start, stop, step):
>        ...
>        append(record)


I knew that trick from 
http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Avoiding_dots... but I 
didn't know about LOAD_ATTR. Is a list comprehension still faster than this? 
Does it also mean that e.g. "from ctypes import *" (--> c_long()) is faster 
than "import ctypes" (--> ctypes.c_long()). I am now putting as much as 
possible in __init__. I don't like the first way of importing at all.

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


[Tutor] Python help

2012-09-15 Thread Daniel Hulse
Hi. I am trying to solve a problem and I'm stuck. The problem is something like 
as x goes up by 1, y goes up by the previous value times 2. I have no idea 
where to start. So lets say x = 10 and y=5, when x=11, why would be equal to  
10.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python help

2012-09-15 Thread Joel Goldstick
On Sat, Sep 15, 2012 at 11:21 AM, Daniel Hulse  wrote:
> Hi. I am trying to solve a problem and I'm stuck. The problem is something 
> like as x goes up by 1, y goes up by the previous value times 2. I have no 
> idea where to start. So lets say x = 10 and y=5, when x=11, why would be 
> equal to  10.


Your question is really one of simple algebra

This looks like homework.  You should explain your problem more
completely, and show any code you have tried to solve your problem
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Semi-OT] Yes or no on using a Graphical IDE?

2012-09-15 Thread Modulok
> Hey all, not trying to contribute to the flames of one graphical IDE over
> another. I'm just trying to figure out if they are worth the learning
> curve? I have been doing most of my work in vi and the graphical IDE I'm
> supposed to use for a class keeps adding crap that I have to erase, and I
> have to move my hands to use the GUI parts instead of just typing.
>
> Is there a point in which a GUI IDE becomes more of a help than a
> hindrance?
>
> Thanks!
>
> Leam

Leam,

It's really a personal taste. I can't speak for others, but this is my basic
setup: I use a highly customised graphical text editor (jEdit) with several
plugins specific to what I'm doing. I guess it kind of equates to a lightweight
IDE. I combine this with an ssh connection to a FreeBSD box for all the
wonderful *nix command line tools, scripts, debuggers, etc that are out there.

I like the idea of an IDE, but haven't met one I really care for. I much prefer
a GUI editor over a purely console based one. That is, I like the ability to
use a mouse to aid in quickly moving text around. I also like the sub-pixel
anti-aliased fonts that you don't always get on a console. (Of course, this
depends on the console.) You can do most things with command line editors,
(some even support using a mouse) but I never found it to be as fast. Again,
it's personal and I'm probably getting off the subject. This is more of a
console vs. GUI debate.

I short, I'd say use whatever you're comfortable with. After that, use what
your friends/co-workers use. The popup auto-complete for class names or
variable names in IDE's is nice and can often save you from having to look
something up, but it isn't critical. Console tools like ipython can make up for
some of that, but even so that's not something I generally write code in.

Perhaps others have more insight.
-Modulok-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] is this use or abuse of __getitem__ ?

2012-09-15 Thread eryksun
On Sat, Sep 15, 2012 at 10:18 AM, Albert-Jan Roskam  wrote:

> Thanks, I hadn't noticed this yet. I am refactoring some of the rest of my 
> code
> and I hadn't run anything yet. My code has two methods that return record(s):
> an iterator (__getitem__) and a generator (readFile, which is also called by
> __enter__). Shouldn't I also take the possibility of a MemoryError into
> account when the caller does something like data[:10**8]? It may no longer fit
> into memory, esp. when the dataset is also wide.

The issue with c_long isn't a problem for a slice since
key.indices(self.nCases) limits the upper bound. For the individual
index you had it right the first time by raising IndexError before it
even gets to the c_long conversion. I'm sorry for wasting your time on
a non-problem. However, your test there is a bit off. A negative index
can be -nCases since counting from the end starts at -1. If you first
do the ternary check to add the offset to a negative index, afterward
you can raise an IndexError if "not 0 <= value < nCases".

As to MemoryError, dealing with gigabytes of data in main memory is
not a problem I've come up against in practice. You might still want a
reasonable upper bound for slices. Often when the process runs out of
memory it won't even see a MemoryError. The OS simply kills it. On the
other hand, while bugs like a c_long wrapping around need to be caught
to prevent silent corruption of data, there's nothing at all silent
about crashing the process. It's up to you how much you want to
micromanage the situation. You might want to check out psutil as a
cross-platform way to monitor the process memory usage:

http://code.google.com/p/psutil

If you're also supporting the iterator protocol with the __iter__
method, then I think a helper _items(start, stop, step) generator
function would be a good idea.

Here's an updated example (not tested however; it's just a suggestion):


import operator

def _items(self, start=0, stop=None, step=1):
if stop is None:
stop = self.nCases

for i in range(start, stop, step):
retcode1 = self.iomodule.SeekNextCase(self.fh, ctypes.c_long(i))
self.caseBuffer, self.caseBufferPtr = self.getCaseBuffer()
retcode2 = self.iomodule.WholeCaseIn(self.fh, self.caseBufferPtr)
record = struct.unpack(self.structFmt, self.caseBuffer.raw)
if any([retcode1, retcode2]):
raise RuntimeError("Error retrieving record %d [%s, %s]" %
(i, retcodes[retcode1], retcodes[retcode2]))
yield record


def __iter__(self):
return self._items()


def __getitem__(self, key):

is_slice = isinstance(key, slice)

if is_slice:
start, stop, step = key.indices(self.nCases)
else:
key = operator.index(key)
start = key + self.nCases if key < 0 else key
if not 0 <= start < self.nCases:
raise IndexError
stop = start + 1
step = 1

records = self._items(start, stop, step)
if is_slice:
return list(records)
return next(records)


> but I didn't know about LOAD_ATTR.

That's the bytecode operation to fetch an attribute. Whether or not
bypassing it will provide a significant speedup depends on what else
you're doing in the loop. If the the single LOAD_ATTR is only a small
fraction of the total processing time, or you're not looping thousands
of times, then this little change is insignificant.


> Is a list comprehension still faster than this?

I think list comprehensions or generator expressions are best if the
evaluated expression isn't too complex and uses built-in types and
functions. I won't typically write a function just to use a list
comprehension for a single statement. Compared to a regular for loop
(especially if append is cached in a fast local), the function call
overhead makes it a wash or worse, even given the comprehension's
efficiency at building the list. If the main work of the loop is the
most significant factor, then the choice of for loop vs list
comprehension doesn't matter much with regard to performance, but I
still think it's simpler to just use a regular for loop. You can also
write a generator function if you need to reuse an iteration in
multiple statements.

> Does it also mean that e.g. "from ctypes import *" (--> c_long()) is
> faster than "import ctypes" (--> ctypes.c_long()). I am now putting as much as
> possible in __init__. I don't like the first way of importing at all.

It's not a good idea to pollute your namespace with "import *"
statements. In a function, you can cache an attribute locally if doing
so will provide a significant speedup. Or you can use a default
argument like this:

def f(x, c_long=ctypes.c_long):
return c_long(x)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

[Tutor] Cube root

2012-09-15 Thread Amanda Colley
Ok, I have to get input from a user  ('enter a number') and then get the
cube root of that number.  I am having trouble with the code to get the
cube root. If anyone can help me solve this I would greatly appreciate it.
('enter a number')
n=number
???  cube root??

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


Re: [Tutor] Cube root

2012-09-15 Thread Dave Angel
On 09/15/2012 05:28 PM, Amanda Colley wrote:
> Ok, I have to get input from a user  ('enter a number') and then get the
> cube root of that number.  I am having trouble with the code to get the
> cube root. If anyone can help me solve this I would greatly appreciate it.
> ('enter a number')
> n=number
> ???  cube root??
>
>

The operator to raise a number to a particular power is **, and it's not
constrained to integer powers.  So 5*2 is 25.  See what's next?

-- 

DaveA

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


Re: [Tutor] Cube root

2012-09-15 Thread Joel Goldstick
On Sat, Sep 15, 2012 at 5:36 PM, Dave Angel  wrote:
> On 09/15/2012 05:28 PM, Amanda Colley wrote:
>> Ok, I have to get input from a user  ('enter a number') and then get the
>> cube root of that number.  I am having trouble with the code to get the
>> cube root. If anyone can help me solve this I would greatly appreciate it.
>> ('enter a number')
>> n=number
>> ???  cube root??
>>
>>
>
> The operator to raise a number to a particular power is **, and it's not
> constrained to integer powers.  So 5*2 is 25.  See what's next?
>
This is a great place to ask questions.  But don't forget google (or bing)

try googling python cube root

see how you do and come back with your code!
-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Scurvy Scott
Hello again python tutor list.
I have what I see as a somewhat complicated problem which I have no idea
where to begin. I'm hoping you fine folks can help me.

I'm trying to generate a list of every possible 16 character string
containing only 2-7 and a-z lowercase. I've seen some examples using regex
to define which characters I want to use but not a way to generate the
complete list of all possibilities. I'm not looking for a handout- just a
point in the right direction.

Any information would be awesome, thanks.

Right now I've got something like:

import random
>>> ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in 
>>> range(16))

Which only prints 1 number, obviously.

or possibly something like this:


def genKey():
hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
return alnum_hash[:16]


Keeping in mind that although I understand this code, I did not write it, I
got it from stackoverflow.

Again any help would be great. Feel free to ask if you must know exactly
what I'm trying to do.

I'm running Ubuntu 12.04 and python 2.7

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Joel Goldstick
On Sat, Sep 15, 2012 at 6:50 PM, Scurvy Scott  wrote:
> Hello again python tutor list.
> I have what I see as a somewhat complicated problem which I have no idea
> where to begin. I'm hoping you fine folks can help me.
>
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase. I've seen some examples using regex
> to define which characters I want to use but not a way to generate the
> complete list of all possibilities. I'm not looking for a handout- just a
> point in the right direction.
>
> Any information would be awesome, thanks.
>
> Right now I've got something like:
>
> import random
 ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
 range(16))
>
>
> Which only prints 1 number, obviously.
>
> or possibly something like this:
>
>
> def genKey():
>
> hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
>
> alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
>
> return alnum_hash[:16]
>
>
> Keeping in mind that although I understand this code, I did not write it, I
> got it from stackoverflow.
>
> Again any help would be great. Feel free to ask if you must know exactly
> what I'm trying to do.
>
check this out:
http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python


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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Peter Otten
Scurvy Scott wrote:

> Hello again python tutor list.
> I have what I see as a somewhat complicated problem which I have no idea
> where to begin. I'm hoping you fine folks can help me.
> 
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase. I've seen some examples using regex
> to define which characters I want to use but not a way to generate the
> complete list of all possibilities. I'm not looking for a handout- just a
> point in the right direction.
> 
> Any information would be awesome, thanks.
> 
> Right now I've got something like:
> 
> import random
 ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
 range(16))
> 
> Which only prints 1 number, obviously.
> 
> or possibly something like this:
> 
> 
> def genKey():
> hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
> alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
> return alnum_hash[:16]
> 
> 
> Keeping in mind that although I understand this code, I did not write it,
> I got it from stackoverflow.
> 
> Again any help would be great. Feel free to ask if you must know exactly
> what I'm trying to do.
> 
> I'm running Ubuntu 12.04 and python 2.7
> 
> Scott

from itertools import product
from string import ascii_lowercase

chars = ascii_lowercase + "234567"
assert len(chars) == 32

for item in product(*[chars]*16):
print "".join(item)

Now, assuming this script will print 10 million 16-character strings per 
second it should terminate in...

>>> 32**16 / (1000*60*60*24*365)
3833478626L

about four billion years.

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Dave Angel
On 09/15/2012 06:50 PM, Scurvy Scott wrote:
> Hello again python tutor list.
> I have what I see as a somewhat complicated problem which I have no idea
> where to begin. I'm hoping you fine folks can help me.
>
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase.

That list would fill all the PC's on the planet a few billions times. 
The number of items in the list has 25 digits in it.  print 32**16

>  I've seen some examples using regex

Not likely to be the least bit useful.

> to define which characters I want to use but not a way to generate the
> complete list of all possibilities. I'm not looking for a handout- just a
> point in the right direction.
>
> Any information would be awesome, thanks.
>
> Right now I've got something like:
>
> import random
 ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in 
 range(16))

You said you wanted a list with every possible string, not a list of
random strings.

> Which only prints 1 number, obviously.
>
> or possibly something like this:
>
>
> def genKey():
> hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
> alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
> return alnum_hash[:16]

still random, does not apply to the problem as stated.

>
> Keeping in mind that although I understand this code, I did not write it, I
> got it from stackoverflow.
>
> Again any help would be great. Feel free to ask if you must know exactly
> what I'm trying to do.
>
> I'm running Ubuntu 12.04 and python 2.7
>
> Scott
>

If you wanted to use a smaller string, or a smaller set of characters,
so that it might be actually possible to list ALL the possibilities,
then start with Peter Otten's code using itertools.product().

import itertools
chars = "ab5"
result = ["".join(item) for item in product(*[chars]*4)]

print len(result)

This one only has 81 items in it ( 3**4 )  so it's quite workable.

if you want to see the first seven items:

for item in result[:7]:
print item


aaab
aaa5
aaba
aabb
aab5
aa5a


-- 

DaveA

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


Re: [Tutor] Cube root

2012-09-15 Thread Steven D'Aprano

On 16/09/12 07:28, Amanda Colley wrote:

Ok, I have to get input from a user  ('enter a number') and then get the
cube root of that number.  I am having trouble with the code to get the
cube root. If anyone can help me solve this I would greatly appreciate it.
('enter a number')
n=number
???  cube root??



The definition of "cube root of x" is "x to the power of one third".

To calculate cube root, you need to get the number from the user and raise
it to the power of one third. This is actually a subtle problem:

* input you get from the user is a string, you need to turn it into a
  number

* you have a choice between using the ** operator or the pow() function
  for raising x to a power

* the obvious solution:

  x**1/3

  is wrong. Can you see why? (Hint: there are two operators there, power
  and divide. What order do they get executed?)

* even more subtle: there is a difference in how Python does division
  depending on the version you use. Calculating one third correctly is
  slightly trickier than it seems.


I hope this is enough hints for you to solve the problem. If not, show
us the code you have and ask for more help.



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


Re: [Tutor] Cube root

2012-09-15 Thread akleider
> On Sat, Sep 15, 2012 at 5:36 PM, Dave Angel  wrote:
>> On 09/15/2012 05:28 PM, Amanda Colley wrote:
>>> Ok, I have to get input from a user  ('enter a number') and then get
>>> the
>>> cube root of that number.  I am having trouble with the code to get the
>>> cube root. If anyone can help me solve this I would greatly appreciate
>>> it.
>>> ('enter a number')
>>> n=number
>>> ???  cube root??
>>>
>>>
>>
>> The operator to raise a number to a particular power is **, and it's not
>> constrained to integer powers.  So 5*2 is 25.  See what's next?
>>
> This is a great place to ask questions.  But don't forget google (or bing)
>
> try googling python cube root
>
> see how you do and come back with your code!
> --
> Joel Goldstick
Why does:
"""
>>> import math
>>> 9**(1/3.0)
2.080083823051904
>>> 9.0**(1.0/3.0)
2.080083823051904
>>>
"""
not seem to work?

>


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


Re: [Tutor] Cube root

2012-09-15 Thread akleider
> On Sat, Sep 15, 2012 at 5:36 PM, Dave Angel  wrote:
>> On 09/15/2012 05:28 PM, Amanda Colley wrote:
>>> Ok, I have to get input from a user  ('enter a number') and then get
>>> the
>>> cube root of that number.  I am having trouble with the code to get the
>>> cube root. If anyone can help me solve this I would greatly appreciate
>>> it.
>>> ('enter a number')
>>> n=number
>>> ???  cube root??
>>>
>>>
>>
>> The operator to raise a number to a particular power is **, and it's not
>> constrained to integer powers.  So 5*2 is 25.  See what's next?
>>
> This is a great place to ask questions.  But don't forget google (or bing)
>
> try googling python cube root
>
> see how you do and come back with your code!
> --
> Joel Goldstick

OOPS!!
Should have been:
>>> 27.0**(1.0/3.0)
3.0
>>>
Which works better than expected!



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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Scurvy Scott
>
> That list would fill all the PC's on the planet a few billions times.
> The number of items in the list has 25 digits in it.  print 32**16
>
> I actually should've specified that the list I'm trying to create would
not start at say "0001".
I'm attempting to generate all possible .onion addressess which look like "
kpvz7ki2v5agwt35.onion" for instance.


The TOR network generates these numbers with this method:
"If you decide to run a hidden service Tor generates an
RSA-1024keypair. The .onion name is
computed as follows: first the
 SHA1  hash of the
DER-encoded
 ASN.1  public
key is calculated. Afterwards the first half of the hash is encoded to
Base32  and the suffix ".onion" is
added. Therefore .onion names can only contain the digits 2-7 and the
letters a-z and are exactly 16 characters long."
-from the Tor Hidden Services DOC page.

I'm not sure if that changes anything as far as the impossible size of my
dataset.

Again, any input is useful.

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider
> Hello again python tutor list.
> I have what I see as a somewhat complicated problem which I have no idea
> where to begin. I'm hoping you fine folks can help me.
>
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase. I've seen some examples using regex
> to define which characters I want to use but not a way to generate the
> complete list of all possibilities. I'm not looking for a handout- just a
> point in the right direction.
>
> Any information would be awesome, thanks.
>
> Right now I've got something like:
>
> import random
 ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
 range(16))
>
> Which only prints 1 number, obviously.
>
> or possibly something like this:
>
>
> def genKey():
> hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
> alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
> return alnum_hash[:16]
>
>
> Keeping in mind that although I understand this code, I did not write it,
> I
> got it from stackoverflow.
>
> Again any help would be great. Feel free to ask if you must know exactly
> what I'm trying to do.
>
> I'm running Ubuntu 12.04 and python 2.7
>
> Scott

This seems to work:
#!/usr/bin/env python

# file :  every.py
print 'Running "every.py"'

possible = "234567abcdefghijklmnopqrstuvwxyz"
word_length = 16

word_list = []
def add_word(word):
if len(word)==word_length:
word_list.append(word)
print word   # There may come a time you won't want this line.
else:
for c in possible:
new_word = word + c
add_word(new_word)

add_word("")

# print word_list




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


Re: [Tutor] [Semi-OT] Yes or no on using a Graphical IDE?

2012-09-15 Thread Steven D'Aprano

On 15/09/12 22:51, leam hall wrote:

Hey all, not trying to contribute to the flames of one graphical IDE over
another. I'm just trying to figure out if they are worth the learning
curve? I have been doing most of my work in vi and the graphical IDE I'm
supposed to use for a class keeps adding crap that I have to erase, and I
have to move my hands to use the GUI parts instead of just typing.

Is there a point in which a GUI IDE becomes more of a help than a hindrance?



Certainly. But where that point is will depend on personal taste and what
other tools you have. If your only tools are Notepad and command.com on
Windows, then an IDE will seem like a *really excellent idea*.

On the other hand, if you have a rich set of powerful editors and command
line tools, then you have a full Development Environment right there, and
adding Integrated doesn't really add much.

It might even subtract. A single Integrated DE can only have a single user-
interface, which may not suit your tastes and ways of working. Having a rich
set of independently configurable tools makes it easier to find your own
personal sweet spot.

So don't knock the power of an Unintegrated Development Environment. Or
rather, a very loosely integrated one, since the integration is done via
the shell.

http://blog.sanctum.geek.nz/series/unix-as-ide/

There are many IDEs for Python, and I suppose some people must like them:

http://wiki.python.org/moin/IntegratedDevelopmentEnvironments
http://wiki.python.org/moin/PythonEditors


Back in ancient days when I was programming on an Apple Macintosh, I
found IDEs to be *very* useful.

To start with, the original Apple Macs had no command line and weren't
multitasking, so everything was a GUI and if you wanted to run an
compiler and an editor at the same time, you needed to have an integrated
compiler/editor. I used Lightspeed Pascal, later known as THINK Pascal,
and it was excellent.

It was also before the term IDE was invented, so I never thought of myself
as using an IDE, I thought all compilers were the same. I was in for
rather a shock when I learned that they weren't.

The editor was fast and responsive, and automatically formatted my code
as I typed. It could pick up syntax errors automatically. There was no
interactive shell, like Python has, but Pascal doesn't really watch that
paradigm very well, and you could run your application live in the IDE
and set break-points to inspect variables using a powerful integrated
debugger.

In my opinion, a good GUI integrated debugger makes or breaks an IDE: I
don't use debuggers enough to learn all the obscure commands for driving
it from the keyboard, so for me the choices are: (1) a good GUI debugger,
or (2) print statements in the code. Currently I use (2).

THINK Pascal didn't have anything like "Intellisense" or code refactoring,
but this was back in 1990 so the technology wasn't really up to it.

Another IDE I used, also back on the Macintosh, was Hypercard, which was
a combined GUI-builder app and runtime environment. The IDE part was even
more primative, but the model of a GUI builder where you can run code is
*fantastic*, and I've never seen anything that comes close since.

I recently decide to start looking at some Python IDE's to see what I'm
missing out on. To start with, forget IDLE that comes with Python. In my
opinion, it's worse than useless. But apparently, some people like it.
Heaven knows why.

I also looked at Spyder. What I found was that it is feature-rich, but
on my laptop the editor was not *quite* responsive enough. I'm not a
touch-typist, but if I were, I would probably notice the lag.

But absolutely deadly in my opinion is that Spyder tries to flag syntax
errors, programming bugs, and style issues as you type. That's not a bad
thing in itself, but there is a second or two lag between me typing
something and the editor flagging lines with little coloured flags and
what-not. As a consequence, as I type I'm constantly being distracted by
bling appearing and disappeared all over the page (not just on the line
I'm typing).

In my opinion, if as-you-type code analysis isn't instantaneous, it is
worse than useless. Some feedback needs to be immediate, within a tenth
of a second, or else must be on-demand only.



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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Scurvy Scott
possible = "234567abcdefghijklmnopqrstuvwxyz"
word_length = 16
print 'Running "every.py"'
word_list = []
def add_word(word):
if len(word)==word_length:
word_list.append(word)
print word
else:
for c in possible:
new_word = word + c
add_word(new_word)


The only problem with this code is that it actually takes a word as its
input and just checks that it is == word length. If not it just appends the
string possible to the variable word that was called in the function
add_word.

I need to be able to generate every possible combination of the characters
in the variable possible, ideally coming up with a string that resembles
the TOR hidden network strings that look like this: "kpvz7ki2v5agwt35.onion"


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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider

#!/usr/bin/env python

# file :  every.py
print 'Running "every.py"'

possible = "234567abcdefghijklmnopqrstuvwxyz"
word_length = 16

word_list = []
def add_word(word):
if len(word)==word_length:
word_list.append(word)
print word   # There may come a time you won't want this line.
else:
for c in possible:
new_word = word + c
add_word(new_word)

add_word("")

# print word_list


> That solution is really nice, thanks, I doubt I would've come up with
> something as nice. What I'll do is have that write to a file then use
> regex
> to strip out any characters like whitespace or commas from the list and
> that should give me a good solid list, no?
>
> I'm using this to write a program I'm calling TORdialer with the goal of
> attempting to basically ping all possible TOR hidden service pages with
> the
> goal of having an absolutely complete list of all TOR hidden services on
> the net right now. Unfortunately it will probably be a lot of fucked up CP
> sites and drug related marketplaces. I'm mostly interested in the sites
> that aren't publicized (ie- government related sites, etc).
> Thanks for your help and I'll try to keep everyone posted.
>

There won't be any whitespace or commas in any of the derived output.
For your purposes, you might want to make this into a generator although
that would be getting too sophisticated for me to help you.
Since in its present form the algorithm is a recursive one, I'm guessing
it'll run out of memory long before it comes to completion although I
haven't let it run for more than a few seconds, just enough to see that it
seemed to be doing what I wanted.
>From what I know about computer science, I believe that most if not all
recursive algorithms can be re-written to be non recursive. If you could
do that, you could then make a generator and presumably make use of it
without overwhelming resources.
I look forward to hearing what those with more expertise might have to say
about that.

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider
> possible = "234567abcdefghijklmnopqrstuvwxyz"
> word_length = 16
> print 'Running "every.py"'
> word_list = []
> def add_word(word):
> if len(word)==word_length:
> word_list.append(word)
> print word
> else:
> for c in possible:
> new_word = word + c
> add_word(new_word)
>
>
> The only problem with this code is that it actually takes a word as its
> input and just checks that it is == word length. If not it just appends
> the
> string possible to the variable word that was called in the function
> add_word.
>
> I need to be able to generate every possible combination of the characters
> in the variable possible, ideally coming up with a string that resembles
> the TOR hidden network strings that look like this:
> "kpvz7ki2v5agwt35.onion"
>
>
> Scott

Actually it doesn't even do that. It only prints "running 'every.py'"!
You forgot the line that does all the work:
"""
add_word("")
"""
I'm attaching the whole file so you don't miss the important bit.
Run it and see what happens, but be ready to CNTL-C (I'm assuming you are
running Linux, I think it'll be the same on Mac, all bets are off
regarding M$ Windoz:-)


Alex
#!/usr/bin/env python

# file :  every.py
print 'Running "every.py"'

possible = "234567abcdefghijklmnopqrstuvwxyz"
word_length = 16

word_list = []
def add_word(word):
if len(word)==word_length:
word_list.append(word)
print word   # There may come a time you won't want this line.
else:
for c in possible:
new_word = word + c
add_word(new_word)

add_word("")

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider
>> Hello again python tutor list.
>> I have what I see as a somewhat complicated problem which I have no idea
>> where to begin. I'm hoping you fine folks can help me.
>>
>> I'm trying to generate a list of every possible 16 character string
>> containing only 2-7 and a-z lowercase. I've seen some examples using
>> regex
>> to define which characters I want to use but not a way to generate the
>> complete list of all possibilities. I'm not looking for a handout- just
>> a
>> point in the right direction.
>>
>> Any information would be awesome, thanks.
>>
>> Right now I've got something like:
>>
>> import random
> ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
> range(16))
>>
>> Which only prints 1 number, obviously.
>>
>> or possibly something like this:
>>
>>
>> def genKey():
>> hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
>> alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
>> return alnum_hash[:16]
>>
>>
>> Keeping in mind that although I understand this code, I did not write
>> it,
>> I
>> got it from stackoverflow.
>>
>> Again any help would be great. Feel free to ask if you must know exactly
>> what I'm trying to do.
>>
>> I'm running Ubuntu 12.04 and python 2.7
>>
>> Scott
>
> This seems to work:
> #!/usr/bin/env python
>
> # file :  every.py
> print 'Running "every.py"'
>
> possible = "234567abcdefghijklmnopqrstuvwxyz"
> word_length = 16
>
> word_list = []
> def add_word(word):
> if len(word)==word_length:
> word_list.append(word)
> print word   # There may come a time you won't want this line.
> else:
> for c in possible:
> new_word = word + c
> add_word(new_word)
>
> add_word("")
>
> # print word_list
>
Addendum:
As others have pointed out, you might run out of patience, time, memory,
or what ever ...long before completion.
one solution would be to make "word_length" &/or "possible" shorter.
It was fun trying to knock this one out.


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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Steven D'Aprano

On 16/09/12 13:06, aklei...@sonic.net wrote:


#!/usr/bin/env python

# file :  every.py
print 'Running "every.py"'

possible = "234567abcdefghijklmnopqrstuvwxyz"
word_length = 16

word_list = []
def add_word(word):
 if len(word)==word_length:
 word_list.append(word)
 print word   # There may come a time you won't want this line.
 else:
 for c in possible:
 new_word = word + c
 add_word(new_word)

add_word("")

# print word_list

[...]

There won't be any whitespace or commas in any of the derived output.
For your purposes, you might want to make this into a generator although
that would be getting too sophisticated for me to help you.
Since in its present form the algorithm is a recursive one, I'm guessing
it'll run out of memory long before it comes to completion although I
haven't let it run for more than a few seconds,


There are 32**16 = 1208925819614629174706176 16-character strings using
digits 2-7 and lowercase a-z. To store a full list of them all will take
at least 74953400816107 terrabytes of memory. Given that even high-end
IBM Blade servers don't support even a single terrabyte of RAM, I think
that your guess about running out of memory is pretty safe...




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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Steven D'Aprano

On 16/09/12 08:50, Scurvy Scott wrote:

Hello again python tutor list.
I have what I see as a somewhat complicated problem which I have no idea
where to begin. I'm hoping you fine folks can help me.

I'm trying to generate a list of every possible 16 character string
containing only 2-7 and a-z lowercase.

[...]

I'm not looking for a handout- just a point in the right direction.



Look at itertools.product for a way to generate such strings one at a time,
without needing to store them all in a list.



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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Dave Angel
On 09/15/2012 10:03 PM, Scurvy Scott wrote:
>>
>> That list would fill all the PC's on the planet a few billions times.
>> The number of items in the list has 25 digits in it.  print 32**16
>>
>> I actually should've specified that the list I'm trying to create would
> not start at say "0001".

Of course not.  Your defined set of characters didn't have a zero in it.

> I'm attempting to generate all possible .onion addressess which look like "
> kpvz7ki2v5agwt35.onion" for instance.
> 

"Look like" is pretty vague.  To me,  .onion certainly
looks like the one you said.  And if it's missing, then you don't have
them all.

> 
> The TOR network generates these numbers with this method:
> "If you decide to run a hidden service Tor generates an
> RSA-1024keypair. The .onion name is
> computed as follows: first the
>  SHA1  hash of the
> DER-encoded
>  ASN.1  public
> key is calculated. Afterwards the first half of the hash is encoded to
> Base32  and the suffix ".onion" is
> added. Therefore .onion names can only contain the digits 2-7 and the
> letters a-z and are exactly 16 characters long."
> -from the Tor Hidden Services DOC page.
> 
> I'm not sure if that changes anything as far as the impossible size of my
> dataset.
> 

I can't see any reason why it changes anything.  The world doesn't have
enough disk space to store *every* address.  If you need to calculate
all of them, you don't have enough time.

You need to rethink whatever your real problem is.  For example, if you
were really trying to crack a safe with 32 numbers on the dial and 16
settings to open it, perhaps you should forget all of it and get some
nitro.  Or a stethoscope.  Or bribe somebody who knows the combination.
 If you have to try all of the combinations systematically, you'll never
get there.

> Again, any input is useful.
> 
> Scott
> 


-- 

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider
> On 09/15/2012 10:03 PM, Scurvy Scott wrote:
>>>
>>> That list would fill all the PC's on the planet a few billions times.
>>> The number of items in the list has 25 digits in it.  print 32**16
>>>
>>
>
> I can't see any reason why it changes anything.  The world doesn't have
> enough disk space to store *every* address.  If you need to calculate
> all of them, you don't have enough time.
>
> You need to rethink whatever your real problem is.  For example, if you
> were really trying to crack a safe with 32 numbers on the dial and 16
> settings to open it, perhaps you should forget all of it and get some
> nitro.  Or a stethoscope.  Or bribe somebody who knows the combination.
>  If you have to try all of the combinations systematically, you'll never
> get there.

We can probably all agree that there aren't enough resources (time,
memory, etc) to solve the problem, but that doesn't make the problem
uninteresting.
What interests me, and I acknowledge that this is more a question for a
computer science forum than a python one, is: can this be done in a non
recursive way so the limiting factor will be time, not memory?  I couldn't
think of a way.




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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Steven D'Aprano

Akleider, you didn't include attribution for the person you are quoting,
which is a bit rude and also means I can't be sure who it was. From
context, I *think* it is the original poster Scott:

On 16/09/12 13:06, aklei...@sonic.net wrote:

I'm using this to write a program I'm calling TORdialer with the goal of
attempting to basically ping all possible TOR hidden service pages with
the goal of having an absolutely complete list of all TOR hidden services
on the net right now.


Did you really think that when a group of really smart security and network
experts sit down to design a protocol for hiding services from discovery,
that it would actually be vulnerable to a programming beginner who just
lists out all the possible sites and pings them? And the TOR designers
didn't think of that?

I'm sorry if this comes across as condescending, but it's kinda cute that
you thought this was viable. TOR is designed to be resistant to discovery
from people with the full resources of the United States and Chinese
governments. Believe me, they already considered the "brute force attack"
where you list out every possible address.

If every person in the world runs their own TOR hidden network, then the
chances of you pinging *even one* by accident is about 1 in 172703688516375.
If your TORdialer program could ping a million addresses per second, you
would need to run it non-stop for over five years just to find *one* address.

"An absolutely complete list"... oh my stars and garters.

(Remember those maths classes on permutations and combinations that you
probably thought were totally pointless? *This* is why they're important.)



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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Dave Angel
On 09/15/2012 11:48 PM, aklei...@sonic.net wrote:
>> On 09/15/2012 10:03 PM, Scurvy Scott wrote:
 That list would fill all the PC's on the planet a few billions times.
 The number of items in the list has 25 digits in it.  print 32**16

>> I can't see any reason why it changes anything.  The world doesn't have
>> enough disk space to store *every* address.  If you need to calculate
>> all of them, you don't have enough time.
>>
>> You need to rethink whatever your real problem is.  For example, if you
>> were really trying to crack a safe with 32 numbers on the dial and 16
>> settings to open it, perhaps you should forget all of it and get some
>> nitro.  Or a stethoscope.  Or bribe somebody who knows the combination.
>>  If you have to try all of the combinations systematically, you'll never
>> get there.
> We can probably all agree that there aren't enough resources (time,
> memory, etc) to solve the problem, but that doesn't make the problem
> uninteresting.
> What interests me, and I acknowledge that this is more a question for a
> computer science forum than a python one, is: can this be done in a non
> recursive way so the limiting factor will be time, not memory?  I couldn't
> think of a way.
>

it can certainly be done non-recursively, as I showed with a relatively
simple list comprehension, borrowing the idea from Peter Otten.

import itertools
chars = "ab5"
result = ["".join(item) for item in itertools.product(*[chars]*4)]

Non-recursive doesn't mean "uses little memory," however.

Since the problem is to build a list, it will use lots of memory.  If
the problem were instead to make a generator that yields all possible
strings,

import itertools
chars = "ab5"
result_generator = ("".join(item) for item in itertools. product(*[chars]*4))

Now, you could write a loop that examined all these items (pretending
you had unlimited time), something like:

for item in result_generator:
 if test_safe(item):
 print item, "cracked the algorithm"

obviously, you'd need a longer chars, and to change *4  to *16, if you
actually wanted to do the original dataset.

And if you don't want to use itertools, you can trivially do it in a
loop, since you're basically just counting, base 32.

-- 

DaveA

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Steven D'Aprano

On 16/09/12 13:48, aklei...@sonic.net wrote:


What interests me, and I acknowledge that this is more a question for a
computer science forum than a python one, is: can this be done in a non
recursive way so the limiting factor will be time, not memory?  I couldn't
think of a way.



Of course. That's what iterators and generators are for: to generate data
lazily, as required, rather than all up front.

Since this project isn't homework and I have no fear that I'm going to teach
the US and Chinese governments how to break the TOR network's security
module, here's how you could do it given enough time.


py> import string, itertools
py> chars = '234567' + string.lowercase
py> chars
'234567abcdefghijklmnopqrstuvwxyz'
py> it = itertools.product(*([chars]*16))
py> next(it)
('2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2')
py> for i in range(5):  # get the next five address
... print(''.join(next(it)) + '.onion')
...
2223.onion
2224.onion
2225.onion
2226.onion
2227.onion


py> # grab 10 thousand addresses
... addresses = list(itertools.islice(it, 1))
py> [''.join(a)+'.onion' for a in addresses[-3:]]  # look at the last three
['2dsn.onion', '2dso.onion', '2dsp.onion']


py> # skip ahead a million addresses, just throwing the values away
... for i in range(100):
... _ = next(it)
...
py> ''.join(next(it))
'yueq'


Like the speedo on your car, each digit takes longer and longer to roll over
as you move towards the left. It took < ten thousand iterations to change the
third digit from the right. It took < a million iterations to change the
fourth digit. To roll over the left-most 2 into a 3 will take 32**15 iterations,
or 37778931862957161709568.



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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread akleider
> Akleider, you didn't include attribution for the person you are quoting,
> which is a bit rude and also means I can't be sure who it was. From
> context, I *think* it is the original poster Scott:
>
> On 16/09/12 13:06, aklei...@sonic.net wrote:
>>> I'm using this to write a program I'm calling TORdialer with the goal
>>> of
>>> attempting to basically ping all possible TOR hidden service pages with
>>> the goal of having an absolutely complete list of all TOR hidden
>>> services
>>> on the net right now.
>
> Did you really think that when a group of really smart security and
> network
> experts sit down to design a protocol for hiding services from discovery,
> that it would actually be vulnerable to a programming beginner who just
> lists out all the possible sites and pings them? And the TOR designers
> didn't think of that?
>
> I'm sorry if this comes across as condescending, but it's kinda cute that
> you thought this was viable. TOR is designed to be resistant to discovery
> from people with the full resources of the United States and Chinese
> governments. Believe me, they already considered the "brute force attack"
> where you list out every possible address.
>
> If every person in the world runs their own TOR hidden network, then the
> chances of you pinging *even one* by accident is about 1 in
> 172703688516375.
> If your TORdialer program could ping a million addresses per second, you
> would need to run it non-stop for over five years just to find *one*
> address.
>
> "An absolutely complete list"... oh my stars and garters.
>
> (Remember those maths classes on permutations and combinations that you
> probably thought were totally pointless? *This* is why they're important.)
>
>
>
> --
> Steven

I do sincerely apologize for anything that was rude. Please believe me, it
was not my intent to be so.

I also apologize for in any way contributing to anything that might be
considered nefarious.  I did not appreciate that the goal of the exercise
was less than honourable.

My interest in this had only to do with how one might come up with all
possible x length strings consisting only of a predefined set of
characters.
Why one might want to do this, was of no interest to me at the time; in
fact I didn't understand what the goal was and I appreciate your
explaining that to me.

Might I also take this opportunity to thank you and all the other
contributers on this mailing list for the friendly helpful place I have
found this list to be.

Sincerely, Alex Kleider

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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Steven D'Aprano

On 16/09/12 14:11, aklei...@sonic.net wrote:

Akleider, you didn't include attribution for the person you are quoting,
which is a bit rude and also means I can't be sure who it was. From
context, I *think* it is the original poster Scott:



I do sincerely apologize for anything that was rude. Please believe me, it
was not my intent to be so.


No problem! It's just a minor faux pas to *not* name the person you are
quoting when replying to email. It just makes it hard for people to know
who said what.


I also apologize for in any way contributing to anything that might be
considered nefarious.  I did not appreciate that the goal of the exercise
was less than honourable.


Again, no, I don't think there's anything nefarious in trying to discover
TOR networks. I'm curious to know what is out there myself. It's just
that the naivety of the original poster, Scott, thinking that this was a
trivial problem to solve when it was *designed* to be massively hard to solve
even for the CIA or Chinese military. I don't think he's a bad guy, just a
bit lacking in numeracy.



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


Re: [Tutor] [Semi-OT] Yes or no on using a Graphical IDE?

2012-09-15 Thread Wayne Werner

On Sat, 15 Sep 2012, leam hall wrote:


Hey all, not trying to contribute to the flames of one graphical IDE over
another. I'm just trying to figure out if they are worth the learning curve? I
have been doing most of my work in vi and the graphical IDE I'm supposed to use
for a class keeps adding crap that I have to erase, and I have to move my hands
to use the GUI parts instead of just typing.

Is there a point in which a GUI IDE becomes more of a help than a hindrance?


If you really like vi, then probably not ;)

I do my Python coding in Vim with some plugins. I use IPython or 
BPython interpreters for quickly running code/examining modules. I search 
duckduckgo for "python somemodule.method" to get docs/helpful examples for 
what I'm looking for.


The only IDE I've seen that seemed like it could be worth it to me is 
WingIDE - because it does offer vim keybindings, and a lot of Python 
specific goodies. But I don't feel like laying down that chunk of change 
since I don't code Python professionally.


The biggest thing that I'm trying to train myself to do now is `import 
pdb` and `pdb.set_trace()` instead of `print(value_im_interested_in)`. 
I've thought about setting up a vim keybinding for F9 to insert a 
pdb.set_trace() line where I'm at... or do some fancy things involving 
other files and :make. But I haven't quite hit that point of frustration 
yet ;)


I write .NET code at work (sadly), so it might be telling that even with 
the ViEmu plugin for Visual Studio, I actually prefer programming in 
Vim (using :make), with ctags, and grep, to using Visual Studio...


Of course YMMV.
-HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Semi-OT] Yes or no on using a Graphical IDE?

2012-09-15 Thread Dave Angel
On 09/16/2012 12:48 AM, Wayne Werner wrote:
> On Sat, 15 Sep 2012, leam hall wrote:
>
>> Hey all, not trying to contribute to the flames of one graphical IDE
>> over
>> another. I'm just trying to figure out if they are worth the learning
>> curve? I
>> have been doing most of my work in vi and the graphical IDE I'm
>> supposed to use
>> for a class keeps adding crap that I have to erase, and I have to
>> move my hands
>> to use the GUI parts instead of just typing.
>>
>> Is there a point in which a GUI IDE becomes more of a help than a
>> hindrance?
>
> If you really like vi, then probably not ;)
>
> I do my Python coding in Vim with some plugins. I use IPython or
> BPython interpreters for quickly running code/examining modules. I
> search duckduckgo for "python somemodule.method" to get docs/helpful
> examples for what I'm looking for.
>
> The only IDE I've seen that seemed like it could be worth it to me is
> WingIDE - because it does offer vim keybindings, and a lot of Python
> specific goodies. But I don't feel like laying down that chunk of
> change since I don't code Python professionally.
>
> The biggest thing that I'm trying to train myself to do now is `import
> pdb` and `pdb.set_trace()` instead of `print(value_im_interested_in)`.
> I've thought about setting up a vim keybinding for F9 to insert a
> pdb.set_trace() line where I'm at... or do some fancy things involving
> other files and :make. But I haven't quite hit that point of
> frustration yet ;)
>
> I write .NET code at work (sadly), so it might be telling that even
> with the ViEmu plugin for Visual Studio, I actually prefer programming
> in Vim (using :make), with ctags, and grep, to using Visual Studio...
>

If i were still coding C++ with MS Visual Studio, I'd probably still be
using Visual Assist, by Whole Tomato. 
http://www.wholetomato.com/default.asp

I had two companies purchase it for me, but I'm not working for either
of them at the moment.  And the last time i did C++ development, it was
for Linux, using gcc.

This product had "intellisense" before Microsoft coined the term, and
hooks into their IDE to provide its enhanced features.

For Python, I've paid for and am using Komodo IDE, from ActivePython. 
http://www.activestate.com/komodo-ide

However, like most others here, i mostly work in my editor (emacs), and
only switch to Komodo when I have a tougher problem to trace.


-- 

DaveA


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


Re: [Tutor] All possible 16 character alphanumeric strings?

2012-09-15 Thread Dave Angel
On 09/16/2012 12:05 AM, Dave Angel wrote:
> On 09/15/2012 11:48 PM, aklei...@sonic.net wrote:
>>> On 09/15/2012 10:03 PM, Scurvy Scott wrote:
> That list would fill all the PC's on the planet a few billions times.
> The number of items in the list has 25 digits in it.  print 32**16
>
>>> I can't see any reason why it changes anything.  The world doesn't have
>>> enough disk space to store *every* address.  If you need to calculate
>>> all of them, you don't have enough time.
>>>
>>> You need to rethink whatever your real problem is.  For example, if you
>>> were really trying to crack a safe with 32 numbers on the dial and 16
>>> settings to open it, perhaps you should forget all of it and get some
>>> nitro.  Or a stethoscope.  Or bribe somebody who knows the combination.
>>>  If you have to try all of the combinations systematically, you'll never
>>> get there.
>> We can probably all agree that there aren't enough resources (time,
>> memory, etc) to solve the problem, but that doesn't make the problem
>> uninteresting.
>> What interests me, and I acknowledge that this is more a question for a
>> computer science forum than a python one, is: can this be done in a non
>> recursive way so the limiting factor will be time, not memory?  I couldn't
>> think of a way.
>>
> it can certainly be done non-recursively, as I showed with a relatively
> simple list comprehension, borrowing the idea from Peter Otten.
>
> import itertools
> chars = "ab5"
> result = ["".join(item) for item in itertools.product(*[chars]*4)]
>
> Non-recursive doesn't mean "uses little memory," however.
>
> Since the problem is to build a list, it will use lots of memory.  If
> the problem were instead to make a generator that yields all possible
> strings,
>
> import itertools
> chars = "ab5"
> result_generator = ("".join(item) for item in itertools. product(*[chars]*4))
>
> Now, you could write a loop that examined all these items (pretending
> you had unlimited time), something like:
>
> for item in result_generator:
>  if test_safe(item):
>  print item, "cracked the algorithm"
>
> obviously, you'd need a longer chars, and to change *4  to *16, if you
> actually wanted to do the original dataset.
>
> And if you don't want to use itertools, you can trivially do it in a
> loop, since you're basically just counting, base 32.
>

A slight addendum.  The other, recursive, implementation ran out of
memory not because of recursion, but because it was building a ginormous
list.  The recursion is limited to 16 levels, as written.

So that algorithm could also be turned into a generator, if we wanted to
be time-limited and not memory limited.



-- 

DaveA

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