Re: [Tutor] Generating unique ID

2009-10-29 Thread Timo

Thanks for all the answers.

I'm using SQLite as database and will try the ROWID.

Timo


Modulok schreef:

I'm writing an application which will hold a database of people. Ofcourse,
people can have the same name, so I want to stock them with an unique ID.

I've searched and found some things:
- uuid.uuid4()
- id(name)
- os.urandom(n)

But they seem overkill to me. Well, maybe not id().

What should I use the best for this? Maybe examples of other programs that
do something alike?



Use the auto-increment feature of your database database management
backend. (Assuming you're using a database backend like MySQL,
postgresql, etc.) In MySQL your database description would look
something like this (with any other fields  you need):

# MySQL table description:

CREATE TABLE IF NOT EXISTS mytable (
   `uid`BIGINT unsigned NOT NULL auto_increment unique,
   `uname`  CHAR(32) NOT NULL default "guest",
PRIMARY KEY (`uid`));


You could use MySQLdb (third party python module) to talk to the MySQL
process with Python. Other database managers have similar abilities.

  

os.urandom(n)
  


Random numbers are random, NOT unique.

If you're using your own backend, like a text file or whatever, stop.
Take the time to learn to use a database manager like postgresql or
MySQL or whatever. They have already solved many of the problems
you're now facing. It will be well worth the time and frustration.

Otherwise, you'll have to parse your database and get the previously
used value and then increment that. However, this solution will fail
if there are multiple processes, or threads accessing the data
concurrently. To solve that problem you'll have to introduce some
manner of mutex to gurantee that only one process has access to the
unique data at any given time. Things get complicated. All of these
problems have already been solved with other database managers. Use
them!

In a pinch, with a low volume database for non-critical data, you
could probably get away with using a Unix epoch style timestamp with
sufficient granularity. But even this is in no way, "guaranteed" to be
unique. It's just, "probably unique". It would look like this:

  

import time
time.time()


1256796357.661967

If it absolutely must be unique, use a database manager that can make
that guarantee.

Best of luck!
-Modulok-
  


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


Re: [Tutor] Sorting points on a 2D plane

2009-10-29 Thread Robert Berman

Kent and Alan,

Thank you both for providing me with tools I can use to develop the sort
portion of my algorithm. They are invaluable. I really appreciate Luke's
willingness to examine and advise on the full algorithm and once it is
written (only the function that determines distance between two points
is written and working) I will definitely add it to pastebin and ask for
your suggestions as well as the suggestions and comments from the group.

Again, thank you for your help.

Robert

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


Re: [Tutor] New to Python

2009-10-29 Thread asterix09

I am running Windows Vista. 

Do you know what remote desktop is? This is the tool I use to connect to my 
other severs on the network or alternatively I use my "Run" option (Start/Run) 
where you add in the IP address and connect to the server.

I want to use python to do this for me. I can do it with a batch file.

This is what I am looking to do.

I have a log file. In this log file contains the following text:

115=ABS
115=DRF
115=HSD

Lets call this log file A.

I am looking to run s script that will search this log file and take all the 
text after the "=" sign (ABS/DRF..etc) and paste that into another text file, 
call it text file B.

Once that it done I will need to compare other log files to file B and any new 
text after "=" that is not in the text file B must be added and anything that 
is must be ignored.

The goal is that in these log files there will be multiple different text for 
115=..etc. I need to create a list/mini database for them. So every day I need 
to run a script against a logfile to search for any new text.

Thanks


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


Re: [Tutor] New to Python

2009-10-29 Thread Tim Golden

asteri...@petlover.com wrote:
I am running Windows Vista. 


OK. Definite points for giving useful info up front.


Do you know what remote desktop is? 


Yes.

This is the tool I use to connect to my other severs on 
the network or alternatively I use my "Run" option (Start/Run) 
where you add in the IP address and connect to the server.


Uh-oh; not so clear. There's quite a difference between rdp and
a file-server connection. But ok...

I want to use python to do this for me. 


To do *what* for you? Control a remote desktop? Start a
remote folder in explorer?


I can do it with a batch file.


Can you show us the batch file?



This is what I am looking to do.


[... snip stuff about a log file ...]

Have I missed something? What does this have to do with the
previous stuff about remote desktops and connecting to
servers?

I'm sorry, asterix09, I'm sure we're willing to help you,
but you seem to be confusing several things at once here.

I'm going to have a stab at what you're talking about and
let's see how close I get:

* You have several log files on some remote machines

* The log files contain lines of text of the form: aaa=bbb

* You want to perform some kind of comparison of this content, building
 up a database of some underspecified structure.

Am I close?

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


Re: [Tutor] Generating unique ID

2009-10-29 Thread Alan Gauld


"Timo"  wrote


I'm using SQLite as database and will try the ROWID.


Take a look at my working with databases topic under the 
heading Linking Data Across Tables which gives an exampler 
and discussion of using an autoincementing key in SqlLite.



--
Alan Gauld
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] PyQT forum?

2009-10-29 Thread Chris Fuller
Start with the main site.  There are links to wikis/mailing lists/etc there.
http://www.riverbankcomputing.co.uk/software/pyqt/

Also, you might be interested in the fully-free alternate, PySide, sponsored 
by Nokia:
http://www.pyside.org/

Cheers


On Wednesday 28 October 2009 11:17, Christopher Spears wrote:
> I'm starting to learn PyQt.  Can anyone recommend a good mailing list or
> forum?
>
> Thanks.
>
> "I'm the last person to pretend that I'm a radio.  I'd rather go out and be
> a color television set." -David Bowie
>
> "Who dares wins"
> -British military motto
>
> "There is no such thing as luck; there is only adequate or inadequate
> preparation to cope with a statistical universe." -Robert A. Heinlein,
> "Have Space Suit - Will Travel"
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] optional sys.argv parsing

2009-10-29 Thread Andre Walker-Loud

Hi All,

I have a simple question.  I am writing a little program that will  
make some plots of data files.  I want to have optional args to pass,  
for example to specify the plot ranges.  I have never written a script/ 
code that takes optional args (but I have used plenty) - so I am  
feeling a little sluggish writing a good sys.argv reader.  I have the  
following few lines I made, but I am wondering if any of you have  
suggestions for how to make this better (ie more slick, more readable,  
more general etc)



Thanks,

Andre


>>>
import sys

if len(sys.argv) < 2:
print('no data file specified')
sys.exit(-1)
elif len(sys.argv) > 2:
if sys.argv.count('-x') > 1:
print('error: multiple instances of "-x xmin xmax"')
sys.exit(-1)
elif sys.argv.count('-x') == 1:
xrange = sys.argv.index('-x')
if sys.argv.count('-y') > 1:
print('error: multiple instances of "-y ymin ymax"')
sys.exit(-1)
elif sys.argv.count('-y') == 1:
yrange = sys.argv.index('-y')
else:
xrange = 0
yrange = 0

if xrange != 0:
xmin = float(sys.argv[xrange+1])
xmax = float(sys.argv[xrange+2])
else:
xmin = "x-min determined from data file"
xmax = "x-max determined from data file"

if yrange != 0:
ymin = float(sys.argv[yrange+1])
ymax = float(sys.argv[yrange+2])
else:
ymin = "y-min determined from data file"
ymax = "y-max determined from data file"

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


Re: [Tutor] optional sys.argv parsing

2009-10-29 Thread Sander Sweers
On Thu, 2009-10-29 at 17:30 -0400, Andre Walker-Loud wrote:
> I have a simple question.  I am writing a little program that will  
> make some plots of data files.  I want to have optional args to pass,  
> for example to specify the plot ranges.  I have never written a script/ 
> code that takes optional args (but I have used plenty) - so I am  
> feeling a little sluggish writing a good sys.argv reader.  I have the  
> following few lines I made, but I am wondering if any of you have  
> suggestions for how to make this better (ie more slick, more readable,  
> more general etc)

You are a perfect candidate for the optparse module [1] which will do
the heavy lifting for you.

Example code relating to your code below.

--
>>> from optparse import OptionParser
>>> parser = OptionParser()
>>> parser.add_option('-f', '--file', action='store', type='string',
dest='filename', help='Explain your filename')

>>> parser.add_option('-x', action='store', type='int', dest='x',
help='Explain your x value')

>>> parser.print_help()
Usage:  [options]

Options:
  -h, --helpshow this help message and exit
  -f FILENAME, --file=FILENAME
Explain your filename
  -x X  Explain your x value
>>>
>>> args = ['-f','somefilename','-x', '25']
>>> opts, args = parser.parse_args(args)
>>> opts.x
25
>>> opts.filename
'somefilename'
>>> type(opts.x)

-

Greets
Sander

[1] http://docs.python.org/library/optparse.html


> import sys
> 
> if len(sys.argv) < 2:
>  print('no data file specified')
>  sys.exit(-1)
> elif len(sys.argv) > 2:
>  if sys.argv.count('-x') > 1:
>  print('error: multiple instances of "-x xmin xmax"')
>  sys.exit(-1)
>  elif sys.argv.count('-x') == 1:
>  xrange = sys.argv.index('-x')
>  if sys.argv.count('-y') > 1:
>  print('error: multiple instances of "-y ymin ymax"')
>  sys.exit(-1)
>  elif sys.argv.count('-y') == 1:
>  yrange = sys.argv.index('-y')
> else:
>  xrange = 0
>  yrange = 0
> 
> if xrange != 0:
>  xmin = float(sys.argv[xrange+1])
>  xmax = float(sys.argv[xrange+2])
> else:
>  xmin = "x-min determined from data file"
>  xmax = "x-max determined from data file"
> 
> if yrange != 0:
>  ymin = float(sys.argv[yrange+1])
>  ymax = float(sys.argv[yrange+2])
> else:
>  ymin = "y-min determined from data file"
>  ymax = "y-max determined from data file"


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


[Tutor] How to manipulate a variable whose value depends on next values of list using LC or reduce()

2009-10-29 Thread Shashwat Anand
Shashwat Anand to Bangalore
show details 5:31 AM (2 minutes ago)

*# 1:*

>>> sum([1, 2, 3], 4)
10

How does it actually work ?
( ( (1 + 2) + 3) + 4)  or ( ( (4 + 1) + 2 + 3)

sum( ) -> sum: (sequence[, start]), so shouldn't 4 be the 'start' that's
second case ?

"Note that sum(range(n), m) is equivalent to reduce(operator.add, range(n),
m)"

>>> sum ( [ [ 1 ], [ 2, 3 ] ], [ ])
[1, 2, 3]
What's happening here exactly ?
[ 1] + [2, 3] = [1, 2, 3] is understandable, but why do we pass a [ ] as a
[start] parameter to do so ?

>>> reduce(operator.mul, [1, 2, 3], 4)
24
how does it works ?
( ( (1 * 2) * 3) * 4) or (((4 * 1) * 2) * 3)

*# 2:*

I wrote an LCM function of mine as follows:

import fractions
def lcm(mylist):
# lcm by defination is Lowest Common Multiple
# lcm (a*b) = a*b / gcd(a*b)
# lcm (a, b, c) = lcm(lcm(a, b), c)
# the function lcm() returns lcm of numbers in a list
# for the special case of two numbers, pass the argument as lcm([a, b])
sol = 1
for i in mylist:
sol = sol * i / fractions.gcd(sol, i)
return sol

print lcm(range(1, 11))  #gives lcm of numbers (1, 2, 3,9 ,10)
print lcm([2, 3]) #gives lcm of two numbers, a special case
print lcm([2, 5, 6, 10])   #gives lcm of a random list


However I also did a dirty hack as an alternate approach :

import fractions
l = [1]
print max( ( l[i-2], l.append(l[-1] * i / fractions.gcd(l[-1], i ) ) ) for i
in range(2, 12) )[0]
# prints the LCM of list (1, 10)

However to shorten the code i made it totally unpythonic.
Can reduce( ) or any other function be of help ?

Let me take a test-case as an example:
I want to multiple all the content of a given list,
now say my list is lst = [2, 3, 9]
I can do:

sol = 1
for i in lst:
sol *= i
print sol

However this can also be done as:

>>>reduce( operator.mul, lst)

Can it be done via List Comprehension or we have to do dirty hacks as
mentioned above :(
Can the LCM function be reduced ?

*The point is if we insert a variable( sol here in both test case and LCM
case), which changes its values dynamically depending upon the next values
of the list, how can I calculate so without going through the long way of
using for-construct*, which makes me feel as though i'm coding in C. reduce(
) or even sum( ) does helps in some cases but it constrains me as per my
requirement. Any pointers ?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to manipulate a variable whose value depends on nextvalues of list using LC or reduce()

2009-10-29 Thread Alan Gauld


"Shashwat Anand"  wrote


sum([1, 2, 3], 4)

10

sum( ) -> sum: (sequence[, start]), so shouldn't 4 be the 'start' that's
second case ?


Try help(sum):


help(sum)

Help on built-in function sum in module __builtin__:

sum(...)
   sum(sequence, start=0) -> value

   Returns the sum of a sequence of numbers (NOT strings) plus the value
   of parameter 'start'.  When the sequence is empty, returns start.






So the badly named start parameter is the starting value of the sum.
So in your case it adds 4+ sum(1,2,3) = 4+6 = 10


sum ( [ [ 1 ], [ 2, 3 ] ], [ ])

[1, 2, 3]
What's happening here exactly ?


Its been given a list of lists so its adding the list elements

[1] + [2,3] -> [1,2,3]

And adding an empty list to a list returns the original list.

[ 1] + [2, 3] = [1, 2, 3] is understandable, but why do we pass a [ ] as 
a

[start] parameter to do so ?


Because the default start is 0 which is not a list so the addition would 
fail.



sum([[1],[2,3]],[])

[1, 2, 3]

sum([[1],[2,3]])

Traceback (most recent call last):
 File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'int' and 'list'





reduce(operator.mul, [1, 2, 3], 4)

24
how does it works ?



help(reduce)

Help on built-in function reduce in module __builtin__:

reduce(...)
   reduce(function, sequence[, initial]) -> value

   Apply a function of two arguments cumulatively to the items of a 
sequence,

   from left to right, so as to reduce the sequence to a single value.
   For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
   1+2)+3)+4)+5).  If initial is present, it is placed before the 
items

   of the sequence in the calculation, and serves as a default when the
   sequence is empty.

So it starts with 4 then applies mul to [1,2,3]

4*1 -> [4,2,3]
4*2 -> [8,3]
8*3 -> 24


I wrote an LCM function of mine as follows:


I'll let somebody else comment, its too late for me! :-)

HTH,


--
Alan Gauld
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


[Tutor] Shortcut key calls TWO instances of IDLE

2009-10-29 Thread Richard D. Moores
I just bought my first laptop, with 64-bit Vista SP1 on it. I'm trying
to learn a bit of Python 3.1.1 using IDLE. I've set up a shortcut to
idle.pyw (with   C:\Python31\pythonw.exe
c:\Python31\Lib\idlelib\idle.pyw   as its Target). The shortcut has a
shortcut key, Ctrl + Alt + F. When I call IDLE by clicking on it in
Explorer, one instance of IDLE opens. But if I call IDLE with the
shortcut key, two instances open. This is only a small annoyance, but
I'm wondering why it happens. Any guesses?

Thanks,

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


Re: [Tutor] optional sys.argv parsing

2009-10-29 Thread Andre Walker-Loud

Hi Sander,


On Thu, 2009-10-29 at 17:30 -0400, Andre Walker-Loud wrote:

I have a simple question.  I am writing a little program that will
make some plots of data files.  I want to have optional args to pass,
for example to specify the plot ranges.  I have never written a  
script/

code that takes optional args (but I have used plenty) - so I am
feeling a little sluggish writing a good sys.argv reader.  I have the
following few lines I made, but I am wondering if any of you have
suggestions for how to make this better (ie more slick, more  
readable,

more general etc)


You are a perfect candidate for the optparse module [1] which will do
the heavy lifting for you.

Example code relating to your code below.


Thanks - yes this is exactly what I need.  I will play around with this.


Thanks again,

Andre






--

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', action='store', type='string',

dest='filename', help='Explain your filename')


parser.add_option('-x', action='store', type='int', dest='x',

help='Explain your x value')


parser.print_help()

Usage:  [options]

Options:
 -h, --helpshow this help message and exit
 -f FILENAME, --file=FILENAME
   Explain your filename
 -x X  Explain your x value


args = ['-f','somefilename','-x', '25']
opts, args = parser.parse_args(args)
opts.x

25

opts.filename

'somefilename'

type(opts.x)


-

Greets
Sander

[1] http://docs.python.org/library/optparse.html



import sys

if len(sys.argv) < 2:
print('no data file specified')
sys.exit(-1)
elif len(sys.argv) > 2:
if sys.argv.count('-x') > 1:
print('error: multiple instances of "-x xmin xmax"')
sys.exit(-1)
elif sys.argv.count('-x') == 1:
xrange = sys.argv.index('-x')
if sys.argv.count('-y') > 1:
print('error: multiple instances of "-y ymin ymax"')
sys.exit(-1)
elif sys.argv.count('-y') == 1:
yrange = sys.argv.index('-y')
else:
xrange = 0
yrange = 0

if xrange != 0:
xmin = float(sys.argv[xrange+1])
xmax = float(sys.argv[xrange+2])
else:
xmin = "x-min determined from data file"
xmax = "x-max determined from data file"

if yrange != 0:
ymin = float(sys.argv[yrange+1])
ymax = float(sys.argv[yrange+2])
else:
ymin = "y-min determined from data file"
ymax = "y-max determined from data file"



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


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