Re: [Tutor] Some questions about my yen-USD.py

2006-09-07 Thread Pawel Kraszewski
Dnia czwartek, 7 września 2006 14:37, Andrei napisał:

  get me into trouble with the flakiness of float(n)? In testing I
  didn't find any problems, but ..

 Nah. Float accuracy is only a problem if you need around lots of
 significant digits (16 or so).

I wouldn't bet. Such a simple thing as 0.1 can't be represented correctly on 
Float... That's what 'decimal' is for.

See that:

 0.1 + 0.1 + 0.1 - 0.3
5.5511151231257827e-17

 from decimal import Decimal
 Decimal(0.1)+ Decimal(0.1)+ Decimal(0.1)-Decimal(0.3)
Decimal(0.0)


For more see: http://docs.python.org/lib/module-decimal.html

-- 
 Pawel Kraszewski
 http://www.kraszewscy.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [tutor] how to cast to stucture

2006-09-05 Thread Pawel Kraszewski
Dnia wtorek, 5 września 2006 08:32, [EMAIL PROTECTED] napisał:

 i have a complex data in binary file and i want to read its
 fields... the C way is to read file in buffer and then cast
 it to proper structure. Is there a way to do the same in
 Python or i have to read the data byte by byte ?

cite:

-
struct -- Interpret strings as packed binary data 
 
 
 This module performs conversions between Python values and C structs 
represented as Python strings. It uses format strings (explained below) as 
compact descriptions of the lay-out of the C structs and the intended 
conversion to/from Python values. This can be used in handling binary data 
stored in files or from network connections, among other sources.
-


see http://docs.python.org/lib/module-struct.html

-- 
 Pawel Kraszewski
 http://www.kraszewscy.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginner: using optional agument in __init__ breaks my code

2006-06-26 Thread Pawel Kraszewski
Dnia niedziela, 25 czerwca 2006 22:45, Bob Gailer napisał:

 To get the behavior I think you want try:

 def __init__(self, q = None):
 if not q: q = []
 self.queue = q

I would disagree... This sure works for empty arguments, but:

 u = [1,2,3]
 a = Queue(u)
 b = Queue(u)
 a.insert(12)
 print b

gives [1, 2, 3, 12] which is still wrong. However assigning self.queue a COPY 
of parameter (empty or not) gives the desired result:

def __init__(self, q = []):
self.queue = q[:]


-- 
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test If File System is mounted in Linux

2006-03-18 Thread Pawel Kraszewski
Dnia piątek, 17 marca 2006 19:22, Bill Campbell napisał:

 If you're sure it's a Linux system, fine.  Like /etc/mtab, this isn't
 portable.  Looking at some of the systems we have here:

Fortezza mentioned it the way I assumed he has Linux:

 If there a semi-standard way to test if a file system has been mounted
 or not using Python? In the Linux command line, I can type mount and
 see all mounted file system, and then see if the one I am looking for is

But, generally - you are right. Each system has its own rules for accessing 
mount table. Anyway - this would be a great exercise to make a portable 
pure-python library to get this information.

-- 
 Pawel
 www.kraszewscy.net
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test If File System is mounted in Linux

2006-03-17 Thread Pawel Kraszewski
Dnia piątek, 17 marca 2006 18:41, Adam napisał:

  The more general problem is to get a list of mounted file systems.

 How about just reading the mtab? That's usually /etc/mtab it should be
 readable as a user and it means you don't have to rely on any other
 programs.

Or always-up-to-date  user readable '/proc/mounts' ?

-- 
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Cannot Understand

2006-03-10 Thread Pawel Kraszewski
Dnia piątek, 10 marca 2006 19:31, Edgar Antonio Rodriguez Velazco napisał:

 f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1

Oh God! This smells lispish! Haven't seen a jevel like this before, but I LOVE 
it!


-
First, let's cut that into pieces:

R1 n-1 + abs(n-1) 

 This is a funny way to test if n1. Indeed, for n1 it works as 2*n-2 
(especially, it is non-zero), for n=1 it gives a constant 0

R2 f(n-1)*n

 It is a formula for you-know-what

R3 1

 Well... This is a ONE, you know.

-
Then, few notes on 'and' and 'or' operators. For correctness of many 
algorithms, programmers introduced 'partial evaluation'. What does it mean?

L1 A and B

When A is FALSE, no matter what the B is, the whole result is FALSE. 
Therefore, we DON'T NEED to evaluate B. When A is TRUE, the result is yet 
unknown and we must evaluate B

L2 A or B

When A is TRUE, no matter what the B is, the whole result is TRUE. Therefore, 
we DON'T NEED to evaluate B. When A is FALSE, the result is yet unknown and 
we must evaluate B

L3 'and' has higher priority than 'or'

-
Putting things together:

f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1

which may be rewritten due to law L3 as

 (R1 and R2) or R3

 First check is R1
 
 If R1==false (it means n=1), then whole brace is automaticaly false due to 
law L1. Moreover, due to the same law, R2 is NOT calculated. We now have the 
value of brace, which is FALSE. We now have FALSE or R3, so due to law L2 
we now must evaluate R3 to get the final (lambda) result. The total result is 
value of R3, this means 1.

 If R1==true (it means n1), due to law L1 we must evaluate R2 to get the 
brace result. This is done by recursively calling lambda function with 
argument n-1. Let's call the returned value a RES. When RES is non-zero (it 
actualy always is, due function it implements) we have non-zero result of the 
whole brace. due to law L2, we don't need to evaluate R3 and calculated 
result RES is the return value of lambda function.


-- 
 Pawel Kraszewski

P.S.

This might be pythonized and de-recursived this way:

 ff = lambda n: reduce(lambda x, y: x*y, xrange(1,n+1))

-
Spoiler below... Scroll down





































Of course, as a last resort this may be rewritten in plan english as:

def f(n):
if n1:
return n*f(n-1)
else:
return 1
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Repeating a routine

2006-02-22 Thread Pawel Kraszewski
Dnia środa, 22 lutego 2006 12:43, John Connors napisał:


 I know goto and gosub are evil, bad habits but I'm starting to miss them.

You SHOULD miss them. GOSUB is called a function call in Python (you call a 
function by its name, rather than by its starting line number)

Someone misinformed you: GOSUB is perfectly good. It's GOTO that's evil.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exception repeated in a loop

2005-12-06 Thread Pawel Kraszewski
Dnia wtorek, 6 grudnia 2005 16:29, Jan Eden napisał:
 Hi,

 I use the following loop to parse some HTML code:

 for record in data:
 try:
 parser.feed(record['content'])
 except HTMLParseError, (msg):
 print !!!Parsing error in, record['page_id'], : , msg

 Now after HTMLParser encounters a parse error in one record, it repeats to
 execute the except statement for all following records - why is that?

Short answer: because you told Python to do so...

Long answer:

My hint for students having such problems is to execute their code with a 
pencil on a hardcopy. They read aloud what the program currently does  - 
usually they spot the error during the first reading.

Your code being read loud

1. begin loop
2.  attempt to execute parser.feed
3.   abort attempt if it fails, showing the error
4. take next loop

So - you take next loop regardless of the failure or not. There are two ways 
out of here. I wrote them aloud, to transcribe into python as an excersize:

(Notice the difference between this and your original)

I)

1. attempt to 
2.  begin loop
3.   abort attempt if it fails, showing the error
4.  take next loop

II)
1. begin loop
2.  attempt to execute parser.feed
3.   abort attempt if it fails, showing the error AND breaking the loop
4. take next loop

Hope this helps,
-- 
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python DB

2005-09-22 Thread Pawel Kraszewski
Dnia czwartek, 22 września 2005 10:43, Matt Williams napisał:

 I've been looking for a simple python based DB for a fairly simple app.
 Only a single user, single access needed, but with a dynamic structure
 (which needs to be extensible very easily).

For pure Python implementation you might try SnakeSQL 
(http://www.pythonweb.org/projects/snakesql/):

---[CITE]---
SnakeSQL is a pure Python SQL database written to remove the dependence of the 
Python Web Modules on 3rd party drivers for non-Python databases like MySQL 
but designed to be a useful database in its own right.
The database supports the simplest possible subset of ANSI SQL 92 including 
NULLs (something other pure Python databases such as Gadfly do not currently 
support) as well as more advanced features such as foreign key constraints 
and simple joins.

The database is fully DB-API 2.0 compliant and is written in layers so that it 
can easily be extended to support different storage mechanisms. Currently 
implemented are a fast binary DBM driver (default) and a slower CSV file 
driver (handy for viewing table contents when designing and developing an 
application or database structure).
---[/CITE]---

If you don't need pure-Python,take a look at gadfly 
(http://gadfly.sourceforge.net)

If the data isn't too big, you may even try XML storage with DOM access (PyXML 
http://pyxml.sourceforge.net/)

HTH,
-- 
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterating over sorted dictionary keys in one line

2005-09-18 Thread Pawel Kraszewski
Dnia niedziela, 18 września 2005 19:20, Marcin Komorowski napisał:

 I know that one of the ways to iterate over sorted dictionary keys is:
 keylist = dictionary.keys()
 keylist.sort()
 for key in keylist:
 ...

Indeed, 'sort' sorts its argument in-place and returns 'none'. Bur Python 2.4+ 
has a new thing: 'sorted'. This leaves its argument intact and returns a 
sorted copy.

for mykey in sorted(dictionary.keys()):
# things to do


See this:

Python 2.4.1 (#1, Jun 19 2005, 01:02:56)
[GCC 3.4.4 (Gentoo 3.4.4, ssp-3.4.4-1.0, pie-8.7.8)] on linux2
Type help, copyright, credits or license for more information.
 a = [4,3,2,1]
 print sorted(a) # sorted FUNCTION returns sorted list
[1, 2, 3, 4]
 a # and original list remains unchanged
[4, 3, 2, 1]
 print a.sort() # sort METHOD returns nothing
None
 a # instead it changes (sorts) its argument
[1, 2, 3, 4]




Best regards

-- 
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] fourier transform

2005-07-30 Thread Pawel Kraszewski
 Hello, I have a signal that I want to do a fourier transform on. I tried
 using FFT.fft(data).real but I don't understand the output.  what is output
 from this function, and why doesn't it require amplitude and time as
 inputs?

Please write the package you use for FFT. Standard Python doesn't have one. 
Perhaps you use some custom Python? Like Enthought Python? If so, the 
exhaustive guide is delivered with it in CHM (windows help) format.

1. In general FFT algorithms require you to supply just N complex values 
(complex contains both amplitude and phase of signal). If you supply real 
values, the system assumes phase=0 for each sample and takes given value for 
amplitude.

2. You DO supply a time information, you just didn't think of it this way: 
each consecutive sample is taken one fixed clock tick later, so position of 
sample in data gives you the time it was taken.

3. Results of FFT are in the form of complex vector of the same length as 
data. Starting from position 0 it gives you an constant (DC factor), position 
1 an amplitude and phase (remember - complex number gives you both amplitude 
and phase) of wave of the length table/2 and so on. If you take real value of 
this, you discard part of the information. AFAIR - taking real part gives you 
sine component, taking imaginary part gives you cosine component, taking 
absolute value gives you total amplitude and taking angle gives you phase.

4. The answer is symmetrical - usually you take only half of it. I don't 
remember the exact difference between the halves, but you may find it in any 
article on FFT.

Best regards,
 Pawel Kraszewski
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Polish translation of my tutor

2004-12-17 Thread Pawel Kraszewski
Dnia pitek, 17 grudnia 2004 20:59, Alan Gauld napisa:

|Are there any Polish speakers on the tutor list who would like
|to check a new version of my tutorial? There are no formal links
|to it yet as there are only a few pages but it can be found at:

That would be me. First of all 

content=text/html; charset=ISO-6682

Ehm... Never heard of ISO-6682... Already contacted the author. The page looks 
OK, when you force Windows CP 1250.

-- 
 Pawel KraszewskiFreeBSD/Linux

E-Mail/Jabber Phone ICQ   GG
   [EMAIL PROTECTED]+48 604 777447   45615564   69381
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor