Re: [Tutor] string to binary and back... Python 3

2012-07-20 Thread Alan Gauld

On 20/07/12 06:48, wolfrage8...@gmail.com wrote:


Thanks I will give this a try. Can you explian a little further for me
what exactly this:

newhexdata = bytes("%x" % numdata, "ascii")
line is doing? I don't quite understand the use of the "%x" % on numdata.


It is standard string formatting in pre Python 3 style.
Thus "%x" says I want a string containing a hex  number
and % numdata says use the value of hexdata to populate
the string.

Using the format method it would look like:

newhexdata = bytes("{0:x}".format(numdata), "ascii")

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] Creating a dictionary on user filter

2012-07-20 Thread Peter Otten
Mike Nickey wrote:

> What I have is this:
> firstList = ['a', 'b', 'c']
> secondList = [1,2,3]
> thirdList = [1.20, 1.23, 2.54]
> 
> What I am looking for is something like this for output:
> {'a': [1, 1.20], 'b': [2, 1.23], 'c': [3, 2.54]}

To get this combine second and third into the list of values and then build 
the final dict using it:

>>> first = ['a', 'b', 'c']
>>> second = [1, 2, 3]
>>> third = [1.20, 1.23, 2.54]
>>> values = zip(second, third)
>>> values
[(1, 1.2), (2, 1.23), (3, 2.54)]
>>> dict(zip(first, values))
{'a': (1, 1.2), 'c': (3, 2.54), 'b': (2, 1.23)}

If tuples as values are not acceptable: 

values = map(list, values)


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


Re: [Tutor] Invalid Token Problem

2012-07-20 Thread Alan Gauld

On 20/07/12 02:01, Ross Wilson wrote:


More specifically, eval() is dangerous if you try to evaluate a string
supplied by someone else.  You really can't predict what will happen.


It really doesn't matter who provides the string, Python and eval() 
don't care. They will behave just as dangerously if you provide the 
wrong string.


And that's the problem because even if you think the string you are 
feeding eval() is safe it only needs a small typo to occasionally

turn it into something not safe - and just once is enough to be painful.

So while eval() introduces security issues where other people try to 
maliciously damage your code, eval() is dangerous even in "normal" use

because it has the potential to do damage.

Think of eval() as being like an old fashioned scythe for cutting hay. A 
scythe is dangerous, used wrongly it can cut off your foot. Used 
correctly it won't. But no matter how long you have been using a

scythe it remains dangerous and you forget that at your peril!

--
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] Join email list

2012-07-20 Thread Alan Gauld

On 19/07/12 22:09, Lily Tran wrote:


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


The server does not respond to subject-line commands, you have to visit 
the web site


--
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] suggestion for an editor

2012-07-20 Thread Steven D'Aprano

Bala subramanian wrote:

Friends,
At present i write programs using vi editor. I am interested to change to
something else. My specific need is that i want to select a portion/small
segment of my program (for eg. a nested loop) and then monitor processing
time it takes for that portion while i run the program. By this i hope to
find the segment that takes time and modify to achieve better speed. Can
someone please share their experience.


I don't think that what you want exists. As far as I know, even full-featured 
IDEs (Integrated Development Environments) don't include profiling of selected 
sections of code.


The only IDEs I am familiar with were from the 1990s, Lightspeed Pascal (later 
Think Pascal), which included integrated editor, compiler, linker and 
debugger. As far as I know while integrated debuggers are still standard, and 
possibly even whole-program profilers, the ability to select a small portion 
of code and profile just that and nothing else is pure fantasy.


But I could be wrong.

If you learn of such an IDE, please come back and share that knowledge with us.




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


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-20 Thread Steven D'Aprano

PyProg PyProg wrote:

Hi all,

I would get a new list as:

[(0, '3eA', 'Dupont', 'Juliette', '11.0/10.0', '4.0/5.0', '17.5/30.0',
'3.0/5.0', '4.5/10.0', '35.5/60.0'), (1, '3eA', 'Pop', 'Iggy',
'12.0/10.0', '3.5/5.0', '11.5/30.0', '4.0/5.0', '5.5/10.0',
'7.5/10.0', '40.5/60.0')]

... from this one:

[(0, '3eA', 'Dupont', 'Juliette', 0, 11.0, 10.0), (0, '3eA', 'Dupont',
'Juliette', 1, 4.0, 5.0), (0, '3eA', 'Dupont', 'Juliette', 2, 17.5,
30.0), (0, '3eA', 'Dupont', 'Juliette', 3, 3.0, 5.0), (0, '3eA',
'Dupont', 'Juliette', 4, 4.5, 10.0), (0, '3eA', 'Dupont', 'Juliette',
5, 35.5, 60.0), (1, '3eA', 'Pop', 'Iggy', 0, 12.0, 10.0), (1, '3eA',
'Pop', 'Iggy', 1, 3.5, 5.0), (1, '3eA', 'Pop', 'Iggy', 2, 11.5, 30.0),
(1, '3eA', 'Pop', 'Iggy', 3, 4.0, 5.0), (1, '3eA', 'Pop', 'Iggy', 4,
5.5, 10.0), (1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0)]

How to make that ? I'm looking for but for now I can't do it.


Good grief! Please take the time to come up with a SIMPLE example! What parts 
of the information there are important and what parts can we ignore?


I can't see anything being flattened there. All I see is duplicates being 
removed. Can you explain what you actually expect?


The best way to remove duplicates will depend on whether or not the items are 
hashable, and whether you care about the order they appear in. Here is a 
simple example of removing duplicates while still keeping the original order:



def remove_dups(sequence):
new = []
for item in sequence:
if item not in new:
new.append(item)
return new


And in use:


py> old = [1, 2, 3, 2, 1, 4, 1, 1, 1, 5, 1, 5, 2]
py> remove_dups(old)
[1, 2, 3, 4, 5]

If you don't care about order, a set will be MUCH faster:

py> set(old)
{1, 2, 3, 4, 5}




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


Re: [Tutor] check against multiple variables

2012-07-20 Thread Walter Prins
On 19 July 2012 23:20, Steven D'Aprano  wrote:
> Selby Rowley-Cannon wrote:
>>
>> I am using a hash table in a small randomization program. I know that some
>> hash functions can be prone to collisions, so I need a way to detect
>> collisions.

> This entire question seems like a remarkable case of premature optimization.
> Start with demonstrating that collisions are an actual problem that need
> fixing.

Unless you know all the keys in advance and have consructed a proven
perfect hash, potential collisions are something that *any* hash table
implementation fundamentally *has* to deal with, no matter how small
the probability of a collision.  (If there's a non-zero probability of
collision you *have* to deal with collisions.)

> Unless you have profiled your application and proven that hash collisions is
> a real problem -- and unless you are hashing thousands of float NANs, that
> is almost certainly not the case -- you are just wasting your time and
> making your code slower rather than faster -- a pessimation, not
> optimization.
>
> And if it *is* a problem, then the solution is to fix your data so that its
> __hash__ method is less likely to collide. If you are rolling your own hash
> method, instead of using one of Python's, that's your first problem.

Unfortunately you usually can't prescribe the data to be stored in a
hash table to accomodate a given hashing function.  ;)  You can
however of course improve your hashing function, or (within reason)
increase the hash table size.

But yes, rolling your own is not a good idea given highly optimized
solutions already exist inside the Python language (dict)  Except of
course if you're studying hash tables as part of some course...

Selby, can you clarify please, are you doing this as part of an
assignment or some coursework?  I can understand your question in the
context of learning/computer studies, but otherwise you should just
use Python's dict.

In any case, to somewhat answer you question, dealing with collisions
in hash tables typically involves some variant of one of the following
2 approaches: 1) Each slot in the hash table is a "bucket", usually
implemented as a list (but might be something else), so you do a
search through the hash bucket to see if the key you'r looking for is
in fact in the "bucket" or 2) You have each slot in the hash table be
a single key/value pair and then once you've hashed you look to see if
the key at the entry identified is the key you're looking for.  If not
(collision), you hash ("probe") again by some factor (modulo the size
of you hash table) and look at the next slot identified etc until you
either find the key you're looking for, or an empty slot.  There many
variations one could come up with.  Have a read of this page on
wikipedia: http://en.wikipedia.org/wiki/Hash_table

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


Re: [Tutor] Problem When Iterating Over Large Test Files

2012-07-20 Thread Steven D'Aprano

Ryan Waples wrote:

I count only 19 lines.


yep, you are right.  My bad, I think I missing copy/pasting line 20.


The first group has only three lines. See below.


Not so, the first group is actually the first four lines listed below.
 Lines 1-4 serve as one group.  For what it is worth, line four should
have 1 character for each char in line 1, and the first line is much
shorter, contains a space, and for this file always ends in either
"1:N:0:" (keep) "1"Y"0:" (remove).   The EXAMPLE data is correctly
formatted as it should be, but I'm missing line 20.



Ah, I had somehow decided that the + was a group delimiter. Which would make 
more sense than having an (apparently) arbitrary plus sign in line 3.


The more information you can supply about the format, the better. Perhaps 
someone will even come up with a standard parser for these files, so that 
every biomed researcher doesn't have to re-invent the wheel every time they 
open one of these files.



[...]

I think you are just reading one frame shifted, its not a well
designed format because the required start character "@", can appear
other places as well


Yes, likely I am reading it shifted, and no, it is not a well-designed format.



I'm pretty sure that my raw IN files are all good, its hard to be sure
with such a large file, but the very picky downstream analysis program
takes every single raw file just fine (30 of them), and gaks on my
filtered files, at regions that don't conform to the correct
formatting.


All I can suggest is that you add more error-checking to your code. For each 
line, or at least for each group of lines, check that the format is as you 
expect, both before and after you write the data.


You might also like to do a disk-check of the disk in question, just in case 
it is faulty and corrupting the data (very unlikely, but it wouldn't hurt to 
check).




 > for reads, lines in four_lines( INFILE ):
ID_Line_1, Seq_Line, ID_Line_2, Quality_Line = lines


Argggh! I screwed that up. Sorry, I missed an extra call. It should be:


for reads, lines in enumerate(four_lines( INFILE )):
ID_Line_1, Seq_Line, ID_Line_2, Quality_Line = lines


Sorry about that.



Can you explain what is going on here, or point me In the right
direction?  I see that the parts of 'lines' get assigned, but I'm
missing how the file gets iterated over and how reads gets
incremented.


There are four things you need to know:


(1) File objects in Python are "iterators" -- the idea is that certain objects 
obey a protocol that says, each time you call the next() function on them, 
they hand over one piece of data, and then advance to the next value. File 
objects are one such iterator: each time you call next(file_object), you get 
one line of text, until EOF.


For-loops naturally work with iterators: under the hood, they repeatedly call 
next() on the object, until the iterator signals it is done.


So instead of the old way:

f = open("myfile", "r")
# grab all the lines at once, if you can
lines = f.readlines()  # hope you don't run out of memory...
for line in lines:
do_something_with(line)


the new way uses less memory and is safer:

f = open("myfile", "r")
for line in f:  # file objects iterate over lines
do_something_with(line)


(2) But what I do is I create my own iterator, called "four_lines", using what 
Python calls a generator. A generator is a special type of function which 
behaves as an iterator: instead of returning a single value and stopping, a 
generator can return multiple values, one at a time, each time you call next() 
on it. The presence of "yield" instead of "return" makes a generator.


So four_lines() is a generator which takes a file object as argument. It 
repeatedly does the following steps:


  a) grab one line from the file object; if that works, great,
 otherwise signal EOF and we're done;

  b) grab three more lines from the file object, but this time
 don't signal EOF, just pad the lines with empty strings;

  c) package those four lines into a tuple of four values and
 yield them (like a return, only the function doesn't exit yet);

  d) surrender control back to the calling code, in this case
 the for-loop;

  e) wait for the next loop, and go back to step 1.

It does this repeatedly until the file object signals EOF, then this also 
signals EOF.



(3) enumerate() is yet another iterator. (You may notice that Python is really 
into iterators and processing data when needed, instead of up-front in a 
list.) What enumerate does is take another iterator as argument, in this case 
the output of four_lines, and simple package that up with a count of how many 
times you've called it. So instead of keeping your own loop variable, 
enumerate does it for you.


An example:

py> for i, x in enumerate(['fe', 'fi', 'fo', 'fum']):
... print(i, x)
...
0 fe
1 fi
2 fo
3 fum


Notice that the count starts at 0 instead of 1. If you don't like that, just 
add one to

Re: [Tutor] Problem When Iterating Over Large Test Files

2012-07-20 Thread Steven D'Aprano

Alan Gauld wrote:

On 19/07/12 07:00, Steven D'Aprano wrote:



 for reads, lines in four_lines( INFILE ):
 ID_Line_1, Seq_Line, ID_Line_2, Quality_Line = lines


Shouldn't that be

  for reads, lines in enumerate( four_lines(INFILE) ):
  ID_Line_1, Seq_Line, ID_Line_2, Quality_Line = lines



It certainly should!

Mea culpa, sorry for any confusion. This is what happens when I don't test 
code before posting.





--
Steven

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


Re: [Tutor] check against multiple variables

2012-07-20 Thread Steven D'Aprano

Walter Prins wrote:

On 19 July 2012 23:20, Steven D'Aprano  wrote:

Selby Rowley-Cannon wrote:

I am using a hash table in a small randomization program. I know that some
hash functions can be prone to collisions, so I need a way to detect
collisions.



This entire question seems like a remarkable case of premature optimization.
Start with demonstrating that collisions are an actual problem that need
fixing.


Unless you know all the keys in advance and have consructed a proven
perfect hash, potential collisions are something that *any* hash table
implementation fundamentally *has* to deal with, no matter how small
the probability of a collision.  (If there's a non-zero probability of
collision you *have* to deal with collisions.)


Absolutely. And did you think that Python, a 20 year old language which uses 
dicts (hash tables) as a fundamental data structure for its own internals, 
somehow neglected to do so? (A rhetorical question, I know you didn't :)


Naturally dictionaries (hash tables) deal with collisions. They couldn't work 
if they didn't.



[...]

But yes, rolling your own is not a good idea given highly optimized
solutions already exist inside the Python language (dict)  Except of
course if you're studying hash tables as part of some course...


Ah, of course that would be an excellent reason for writing your own hash 
table implementation! I didn't think of that at the time.




--
Steven

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


[Tutor] Where to put small auxiliary function

2012-07-20 Thread Jose Amoreira
Hi.
This is a question about style. I have a class definition that calls a
small auxiliary function. Because this function isn't used anywhere
else, I'd like to include it inside the class definition. However, if
I do that, I'll have to use "self" in the call argument, which is (I
think) rather awkward.
Let me give an example:

def is_odd(k):
if k % 2 == 0:
return False
else:
return True

class MyOddNbr(object):
def __init__(self, k):
if is_odd(k):
self.k = k
else:
self.k = k + 1

This works fine, but I'd like to have is_odd defined inside the class
definition, because that's the only context where that function is
used. That would be something like

class MyOddNbr(object):
def is_odd(self,k):
if k % 2 == 0:
return False
else:
return True
def __init__(self,k):
if self.is_odd(k):
self.k = k
else:
self.k = k + 1

This also works fine, but the function is_odd() is so simple and
generic that I find it strange to define it with is_odd(self,k) or to
call it with is_odd(self,k).
What is the pythonic way of doing this kind of stuff?
Thanks.
Ze
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where to put small auxiliary function

2012-07-20 Thread Mark Lawrence

On 20/07/2012 11:07, Jose Amoreira wrote:

Hi.
This is a question about style. I have a class definition that calls a
small auxiliary function. Because this function isn't used anywhere
else, I'd like to include it inside the class definition. However, if
I do that, I'll have to use "self" in the call argument, which is (I
think) rather awkward.
Let me give an example:

def is_odd(k):
 if k % 2 == 0:
 return False
 else:
 return True


I'll point out before anyone else does that you can write this function 
as a one line return.  I'll leave you to work out how.  Personally I 
prefer the longer version but each to their own.




class MyOddNbr(object):
 def __init__(self, k):
 if is_odd(k):
 self.k = k
 else:
 self.k = k + 1

This works fine, but I'd like to have is_odd defined inside the class
definition, because that's the only context where that function is
used. That would be something like

class MyOddNbr(object):
 def is_odd(self,k):
 if k % 2 == 0:
 return False
 else:
 return True
 def __init__(self,k):
 if self.is_odd(k):
 self.k = k
 else:
 self.k = k + 1

This also works fine, but the function is_odd() is so simple and
generic that I find it strange to define it with is_odd(self,k) or to
call it with is_odd(self,k).
What is the pythonic way of doing this kind of stuff?


Don't put it in the class.  It's a general purpose function that can be 
used anywhere so keep it at the module level.



Thanks.
Ze
___
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] Invalid Token Problem

2012-07-20 Thread Ross Wilson

On 20/07/12 17:25, Alan Gauld wrote:

On 20/07/12 02:01, Ross Wilson wrote:


More specifically, eval() is dangerous if you try to evaluate a string
supplied by someone else.  You really can't predict what will happen.


It really doesn't matter who provides the string, Python and eval()
don't care. They will behave just as dangerously if you provide the
wrong string.


But what is the difference if I write incorrect code and *execute* it or 
write an the same code in a string and *eval()* it.  The result is the 
same whether eval() is used or not.  Same result, same risk.


Yes, beginners should be told that eval() is advanced, a little tricky 
and is not usually required.  But "dangerous"?


The risk of eval() (and exec()) is the disconnect when the string is 
supplied by someone else or from another distant part of an application, 
but it's no more 'dangerous' than if I had written the incorrect code 
directly.


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


Re: [Tutor] Where to put small auxiliary function

2012-07-20 Thread Joel Goldstick
On Fri, Jul 20, 2012 at 6:32 AM, Mark Lawrence  wrote:
> On 20/07/2012 11:07, Jose Amoreira wrote:
>>
>> Hi.
>> This is a question about style. I have a class definition that calls a
>> small auxiliary function. Because this function isn't used anywhere
>> else, I'd like to include it inside the class definition. However, if
>> I do that, I'll have to use "self" in the call argument, which is (I
>> think) rather awkward.
>> Let me give an example:
>>
>> def is_odd(k):
>>  if k % 2 == 0:
>>  return False
>>  else:
>>  return True
>
>
> I'll point out before anyone else does that you can write this function as a
> one line return.  I'll leave you to work out how.  Personally I prefer the
> longer version but each to their own.

I like return k % 2


>
>
>>
>> class MyOddNbr(object):
>>  def __init__(self, k):
>>  if is_odd(k):
>>  self.k = k
>>  else:
>>  self.k = k + 1

   class MyOddNbr(object):
 def __init__(self, k):
 self.k = k + k % 2


>>
>> This works fine, but I'd like to have is_odd defined inside the class
>> definition, because that's the only context where that function is
>> used. That would be something like
>>
>> class MyOddNbr(object):
>>  def is_odd(self,k):
>>  if k % 2 == 0:
>>  return False
>>  else:
>>  return True
>>  def __init__(self,k):
>>  if self.is_odd(k):
>>  self.k = k
>>  else:
>>  self.k = k + 1
>>
>> This also works fine, but the function is_odd() is so simple and
>> generic that I find it strange to define it with is_odd(self,k) or to
>> call it with is_odd(self,k).
>> What is the pythonic way of doing this kind of stuff?
>
>
> Don't put it in the class.  It's a general purpose function that can be used
> anywhere so keep it at the module level.
>
>
>> Thanks.
>> Ze
>> ___
>> 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



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


Re: [Tutor] Where to put small auxiliary function

2012-07-20 Thread Jose Amoreira
Hi Mark,
Thanks.

> [SNIP]
>> Let me give an example:
>>
>> def is_odd(k):
>>  if k % 2 == 0:
>>  return False
>>  else:
>>  return True
>
>
> I'll point out before anyone else does that you can write this function as a
> one line return.  I'll leave you to work out how.  Personally I prefer the
> longer version but each to their own.
>

OK, but if I wrote it as an one-liner, then it wouldn't be much use as
an example for my question...

>[SNIP]
> Don't put it in the class.  It's a general purpose function that can be used
> anywhere so keep it at the module level.
>

OK, thanks. That was the kind of advice I was hoping for.
Ze
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where to put small auxiliary function

2012-07-20 Thread Mark Lawrence

On 20/07/2012 12:20, Jose Amoreira wrote:

Hi Mark,
Thanks.


[SNIP]

Let me give an example:

def is_odd(k):
  if k % 2 == 0:
  return False
  else:
  return True



I'll point out before anyone else does that you can write this function as a
one line return.  I'll leave you to work out how.  Personally I prefer the
longer version but each to their own.



OK, but if I wrote it as an one-liner, then it wouldn't be much use as
an example for my question...


I didn't say write it as a one-liner, I said write it as a one line 
return.  Joel Goldstick has provided that separately, although I confess 
that I havn't had it independantly tested :)





[SNIP]
Don't put it in the class.  It's a general purpose function that can be used
anywhere so keep it at the module level.



OK, thanks. That was the kind of advice I was hoping for.
Ze
___
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] Where to put small auxiliary function

2012-07-20 Thread Steven D'Aprano

Jose Amoreira wrote:

Hi.
This is a question about style. I have a class definition that calls a
small auxiliary function. Because this function isn't used anywhere
else, I'd like to include it inside the class definition. 



*shrug*

Then do so.

class Whatever:
@staticmethod
def is_odd(k):
return k % 2 == 1


but really, this is Python, not Java. Feel free to write top level functions.



--
Steven

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


[Tutor] Where do I start developing from?

2012-07-20 Thread Santosh Kumar
Hello There,

First time I came in contact with Python programming languages was
nearly 1 year (I am learning this languages from my home). Prior to
this I was good at PHP, HTML and CSS (this doesn't mean that I want to
say that I wanted to become a web developer). I have read many books
and tutorial that which were available for free
,
,
.

Now I feel bored to learn more and want to implement my knowledge
somewhere. I have no idea what to do, I probably won't program a
software from starch. I want to test how much I am up to. So, where
can I start?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where do I start developing from?

2012-07-20 Thread leam hall
Santosh,

It is fun to try new activities. Maybe implement some of the PHP
things you did in Python. I find myself making small games or tools
for games when I want to try something out.

You might also want to work with Eclipse and some of the IDE tools, as
well as learn Unittesting and stuff like that.

Leam

On 7/20/12, Santosh Kumar  wrote:
> Hello There,
>
> First time I came in contact with Python programming languages was
> nearly 1 year (I am learning this languages from my home). Prior to
> this I was good at PHP, HTML and CSS (this doesn't mean that I want to
> say that I wanted to become a web developer). I have read many books
> and tutorial that which were available for free
> ,
> ,
> .
>
> Now I feel bored to learn more and want to implement my knowledge
> somewhere. I have no idea what to do, I probably won't program a
> software from starch. I want to test how much I am up to. So, where
> can I start?
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


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


Re: [Tutor] suggestion for an editor

2012-07-20 Thread Wayne Werner



On Fri, 20 Jul 2012, Steven D'Aprano wrote:


Bala subramanian wrote:

Friends,
At present i write programs using vi editor. I am interested to change to
something else. My specific need is that i want to select a portion/small
segment of my program (for eg. a nested loop) and then monitor processing
time it takes for that portion while i run the program. By this i hope to
find the segment that takes time and modify to achieve better speed. Can
someone please share their experience.


I don't think that what you want exists. As far as I know, even full-featured 
IDEs (Integrated Development Environments) don't include profiling of 
selected sections of code.


It *would* be possible with a programmers editor (like Vim/Emacs) to write 
a macro or some type of extension to mark a block of code and have it 
profiled for you. But this would most likely consist of taking the exising 
code, finding the place where it's called in your application, and 
replacing that with a call through timeit, executing the app, and then 
removing the code (or do that in a temp file).


But you could also write a fairly simple Python script to do that for you, 
without any type of IDE integration.


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


Re: [Tutor] string to binary and back... Python 3

2012-07-20 Thread eryksun
On Fri, Jul 20, 2012 at 3:16 AM, Alan Gauld  wrote:
> On 20/07/12 06:48, wolfrage8...@gmail.com wrote:
>
> Using the format method it would look like:
>
> newhexdata = bytes("{0:x}".format(numdata), "ascii")

binascii.unhexlify needs an even number of hexadecimal digits (two per
byte). So you need to either modify the above string to prepend a '0'
if the length of newhexdata is odd, or calculate the size ahead of
time and format with zero padding:

nbytes = (numdata.bit_length() + 7) // 8
size = nbytes * 2
newhexdata = bytes('{0:0{1}x}'.format(numdata, size), 'ascii')

Though creating an intermediate string of hex digits seems the long
way around. In Python 3, I'd skip binascii and instead use
int.from_bytes and int.to_bytes:

>>> numdata = 0x0102
>>> nbytes = (numdata.bit_length() + 7) // 8
>>> numdata.to_bytes(nbytes, 'big')
b'\x01\x02'

That said, it still seems more reasonable and flexible to me to use a
generator expression to do the XOR byte by byte instead of turning the
message into a big integer. You could share a large file of random
data and send a starting position along with the encrypted text. Best
of luck.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] suggestion for an editor

2012-07-20 Thread Walter Prins
On 20 July 2012 14:07, Wayne Werner  wrote:
>>> At present i write programs using vi editor. I am interested to change to
>>> something else. My specific need is that i want to select a portion/small
>>> segment of my program (for eg. a nested loop) and then monitor processing
>>> time it takes for that portion while i run the program. By this i hope to
>>> find the segment that takes time and modify to achieve better speed. Can
>>> someone please share their experience.
>>
>> I don't think that what you want exists. As far as I know, even
>> full-featured IDEs (Integrated Development Environments) don't include
>> profiling of selected sections of code.
>
> But you could also write a fairly simple Python script to do that for you,
> without any type of IDE integration.

Your comment made me think that perhaps there's a decorator for this
type of thing, so I googled it and and it turns out there is:
http://mg.pov.lt/profilehooks/

More ideas in a similar vein:
http://stackoverflow.com/questions/5375624/a-decorator-that-profiles-a-method-call-and-logs-the-profiling-result
http://code.activestate.com/recipes/577817-profile-decorator/

HTH,

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


[Tutor] A simple AJAX script needed

2012-07-20 Thread Surya K

Here is what i need:
I want to create a HTML button.. when clicked (should only work for once) 
should invoke a python script. 

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


Re: [Tutor] Invalid Token Problem

2012-07-20 Thread Alan Gauld

On 20/07/12 11:31, Ross Wilson wrote:


But what is the difference if I write incorrect code and *execute* it or
write an the same code in a string and *eval()* it.  The result is the
same whether eval() is used or not.  Same result, same risk.



No, a much bigger risk because you can manipulate your strings at run 
time before eval()ing them.


The potential exists to modify your string in an unexpected way (via 
buggy code or buggy input data) that results in a dangerous command
set being executed, even if not intended. You can't do that in plain 
code unless you are writing self modifying code - and that's even more 
dangerous than using eval()!


I don;t want to exaggerate the risk, it is a lot lower than allowing 
anyone to type in potentially malicious code but it is still a whole 
level more dangerous than typing in explicit code and executing it in 
the interpreter. It's important not to forget that it's not just 
stranger's strings that can cause problems in eval()/exec().


--
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] Where do I start developing from?

2012-07-20 Thread Alan Gauld

On 20/07/12 12:36, Santosh Kumar wrote:


Now I feel bored to learn more and want to implement my knowledge
somewhere. I have no idea what to do, I probably won't program a
software from starch. I want to test how much I am up to.


Have you tried the Python Challenge yet?

Google it.

It will take you on a programming game that teaches a new skill at each 
level. If you complete all levels you'll be a fairly competent python 
programmer, ready to tackle bigger projects...


It should stop you being bored at the very least! :-)


--
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] A simple AJAX script needed

2012-07-20 Thread Alan Gauld

On 20/07/12 16:41, Surya K wrote:

Here is what i need:

I want to create a HTML button.. when clicked (should only work for
once) should invoke a python script.


So what bit do you need help on?
Can you create a web page?
Can you create a form?
Can you script a button?

Can you write a Python web app?
- using which framework?

You don't need AJAX to do what you ask. Simple CGI would work or any 
other web framework. But many frameworks support Ajax. But all do it 
differently.


But very little of this has to do with learning Python itself.


--
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] suggestion for an editor

2012-07-20 Thread Alan Gauld

On 20/07/12 09:43, Steven D'Aprano wrote:


I don't think that what you want exists. As far as I know, even
full-featured IDEs (Integrated Development Environments) don't include
profiling of selected sections of code.


There are a few commercial IDEs that can do this but I've only seen it 
for compiled languages and Smalltalk. And the IDEs were all megabucks (I 
think the one I used was about $3000/seat or $100k for a site license)


I don't know of anything like that for Python (or even for Java).

--
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] Where do I start developing from?

2012-07-20 Thread Mark Lawrence

On 20/07/2012 12:36, Santosh Kumar wrote:

Hello There,

First time I came in contact with Python programming languages was
nearly 1 year (I am learning this languages from my home). Prior to
this I was good at PHP, HTML and CSS (this doesn't mean that I want to
say that I wanted to become a web developer). I have read many books
and tutorial that which were available for free
,
,
.

Now I feel bored to learn more and want to implement my knowledge
somewhere. I have no idea what to do, I probably won't program a
software from starch. I want to test how much I am up to. So, where
can I start?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



This should keep you off the streets 
http://mail.python.org/mailman/listinfo/core-mentorship


--
Cheers.

Mark Lawrence.

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


[Tutor] Rapidly Importing CSVs with Python into MySQL

2012-07-20 Thread Fred G
Hi--

This question has to do with MySQL but is fundamentally a python question,
so apologies if it seems tangential initially...

I've written a long python file that creates all the tables in database UN.
 Here is the first few lines:
import MySQLdb as mysql
statement = """CREATE DATABASE UN"""
db = mysql.connect(this stuff is correct)
cursor = db.connect()
cursor.execute(statement)
sql = """CREATE TABLE UN.1(
 id VARCHAR(255),
 name VARCHAR(255),
 age INT
   )"""
cursor.execute(sql)
statement = """LOAD DATA LOCAL INFILE...(this stuff is correct)"""
db.commit()
db.close()

db = mysql.connect(this stuff is correct)
cursor = db.connect()
sql = """CREATE TABLE UN.2(
 id VARCHAR(255),
 fname VARCHAR(255),
 lname VARCHAR(255),
 age INT
   )"""
cursor.execute(sql)
statement = """LOAD DATA LOCAL INFILE...(this stuff is correct)"""
db.commit()
db.close()

UN.3, UN.4, UN.5...etc...

I have a lot of tables here and I have a few questions:
1) When I run this Python file, only the first table is created.  Why does
the interpreter think that the statements are done at this point? I tried
experimenting with taking out all the "db.close()" commands until the last
one, but that didn't work, either.  More importantly, how can I get it such
that I can press run all and all these tables are built?  I'm going to be
re-building these (and other database's tables) pretty frequently, so it's
pretty frustrating to have to copy and paste out of the .py file into the
idle editor every single statement...

2) Is there a shortkey command to run line-by-line in a file using IDLE in
Python 2.7? (Some languages have this capacity, but a Google search of
python was not too promising about this feature...)

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


Re: [Tutor] Rapidly Importing CSVs with Python into MySQL

2012-07-20 Thread Walter Prins
Hi Fred,

On 20 July 2012 21:34, Fred G  wrote:
> Here is the first few lines:

Rather than paraphrase and omit details that might be relevant to the
problem you're having, please try to whittle down your code to a bare
test program that is runnable by us as-is that demonstrates your
problem.



There's nothing there to suggest that Python or MySQL should thing
that the statements are done, so I'm supposing some of the stuff you
think is correct may not in fact be.  (But this is all guessing also)

> I have a lot of tables here and I have a few questions:
> 1) When I run this Python file, only the first table is created.  Why does
> the interpreter think that the statements are done at this point? I tried
> experimenting with taking out all the "db.close()" commands until the last
> one, but that didn't work, either.

Have you tried moving some of the create statements to a prior section
and creating them with the same connection/cursor you use for the
first table that is created correctly?

> More importantly, how can I get it such
> that I can press run all and all these tables are built?  I'm going to be
> re-building these (and other database's tables) pretty frequently, so it's
> pretty frustrating to have to copy and paste out of the .py file into the
> idle editor every single statement...

Of course.  There's a bug or bugs and you/we need to fix it. :)

> 2) Is there a shortkey command to run line-by-line in a file using IDLE in
> Python 2.7? (Some languages have this capacity, but a Google search of
> python was not too promising about this feature...)

On my Python 2.7 installation (Activestate distribution, in case it
matters), you can run a program line by line in the debugger from Idle
as follows:
1) Open Idle
2) Open the file you want to debug. (File->Open, click filename etc.)
3) On the main interpreter window, click "Debug" then click
"Debugger".  The debugger window appears.
4) Click back to your program window, and press F5 to run it.  The
program is started on the first line in the debugger window and
displays the first line of the program about to be executed.
5) You can now click the buttons at the top to control the program
flow, e.g. "Go", "Step", "Over", "Out" and "Quit", which respectively
(I guess) runs the program without further interruption, steps to the
next line (into functions/methods if needed), steps to the next line
(over function calls if needed), steps to the next line (returning
from the current call, e.g. "out"), and quits the program.

As an aside, I normally use Eclipse with PyDev which has very good
Python debugger integration.  If you'd like to give Eclipse a whirl
sometime and need a hand getting Eclipse set up for Python development
I'd be happy to post a little howto. (It does mean you'll have to
download Java if you don't yet have it, as well as an Eclipse bundle
etc, all of which may perhaps make it less attractive for you, but if
you happen to have a reasonably fast internet connection it's not
*that* much of a hardship...)

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