Re: [Tutor] New to programming and Python

2006-10-26 Thread rolando
Jorge Azedo escreveu:
> Hi guys ( and gals )
>
> I'm totally new to the whole programming scene (I decided to enter it 
> for many reasons, one of which is the fact that I *want* to know how to 
> program my computer) and I decided to start out with Python. I'm reading 
> lots of tutorials on it, trying to understand how to work with it, but 
> for some reason or another, it all seems a great mess to me. Am I doing 
> something wrong? Also, can anyone give me any pointers on how to start 
> working with Python?
>
> Thanks in advance for any help you can give me
> - Jorge
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>   
Well, I´m also kind of new to the scene, so I still don´t know much.

But you can try this book that I´ve been reading:
Dive Into Python:
http://www.diveintopython.org/ (you can download the PDF from there, 
which I belive has already a group of exemples to see when using the book).

Also I´ve been reading the Python Wikibook that you can download from here:
http://en.wikibooks.org/wiki/Python (it still has a few parts missing 
but it can give you a few pointers).


(The part below problaby won´t make sense except if you are portuguese)

I don´t know what type of Netcabo you have, if you want I can upload the 
pdf to a national host to count national traffic :)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to programming and Python (Jorge Azedo)

2006-10-26 Thread rolando
I tought that we could only speak English in here :D

Also I'm newer to python, about a mounth and a half since I started 
studing it.

[EMAIL PROTECTED] escreveu:
> Parece que ja temos elementos para estabelecer uma comunidade portuguesa de
> Python ;-)
>
> Jorge,
>
> Sugiro os tutoriais do site www.devshed.com. são simples e explicam bem o
> significado do codigo!
>
> Tb sou novo na programação, tenho ai 6 meses de Python.
>
> A Caixa Magica promove cursos de Python em Lisboa, este ano houve ja 2 de 3
> dias, penso que em Fevereiro e Junho.
>
> Força, isto no inicio parece confuso, mas breve faz-se luz!
>
> Paulino
>
>
> Citando [EMAIL PROTECTED]:
>
>   
>>>2. New to programming and Python (Jorge Azedo)
>>>   
>>7. Re: New to programming and Python (rolando)
>>
>>
>> Date: Thu, 26 Oct 2006 09:44:41 +0100
>> From: rolando <[EMAIL PROTECTED]>
>> Subject: Re: [Tutor] New to programming and Python
>> To: tutor@python.org
>> Message-ID: <[EMAIL PROTECTED]>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>> Jorge Azedo escreveu:
>> 
>>> Hi guys ( and gals )
>>>
>>> I'm totally new to the whole programming scene (I decided to enter it
>>> for many reasons, one of which is the fact that I *want* to know how to
>>> program my computer) and I decided to start out with Python. I'm reading
>>> lots of tutorials on it, trying to understand how to work with it, but
>>> for some reason or another, it all seems a great mess to me. Am I doing
>>> something wrong? Also, can anyone give me any pointers on how to start
>>> working with Python?
>>>
>>> Thanks in advance for any help you can give me
>>> - Jorge
>>> ___
>>> Tutor maillist  -  Tutor@python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>>
>>>   
>> Well, I?m also kind of new to the scene, so I still don?t know much.
>>
>> But you can try this book that I?ve been reading:
>> Dive Into Python:
>> http://www.diveintopython.org/ (you can download the PDF from there,
>> which I belive has already a group of exemples to see when using the book).
>>
>> Also I?ve been reading the Python Wikibook that you can download from here:
>> http://en.wikibooks.org/wiki/Python (it still has a few parts missing
>> but it can give you a few pointers).
>>
>>
>> (The part below problaby won?t make sense except if you are portuguese)
>>
>> I don?t know what type of Netcabo you have, if you want I can upload the
>> pdf to a national host to count national traffic :)
>>
>>
>> --
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>> End of Tutor Digest, Vol 32, Issue 110
>> **
>>
>> 
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>   

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


Re: [Tutor] Mailing list question

2006-10-26 Thread rolando
I just use the reply button in my Thunderbird, and then change the email 
I want to send to tutor@python.org



Jorge Azedo escreveu:
> Danny Yoo wrote:
>   
>> 
 Not so much a question about Python, but here goes:

 How do I reply to a specific thread in the mailing list? If I place 
 "Re:bla bla"  in the subject line, I notice that I start a new 
 thread, I don't continue one that already exists. How do I go about 
 doing this? Thanks for any info
 
>>> What e-mail client are you using?  Most have a reply-to-all button 
>>> you can use so that you won't have to start a new message to reply to 
>>> someone.
>>>   
>> There's a little bit of extra information that Mailman uses to detect 
>> threads.  The archives don't cluster messages based on the subject 
>> line, but instead use a specific header value whose name I'm 
>> completely forgetting right now... *grin* (I think it's the 
>> 'In-Reply-To' header line.)  Your email client should be the one 
>> responsible for maintaining that threading information.
>>
>> 
> I'm trying to use the Reply All button on my mail client (I use 
> Thunderbird, by the way) like you guys suggested. Let's see if it works  
> :-P
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>   

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


Re: [Tutor] Mailing list question

2006-10-27 Thread rolando
Oh, ok :D


Luke Paireepinart escreveu:
> rolando wrote:
>> I just use the reply button in my Thunderbird, and then change the 
>> email I want to send to tutor@python.org
> It's better to use reply-all, in my opinion, because then, if 
> someone's involved in an e-mail thread, they'll get an instant
> update on it even if they only get the Tutor Digest and not every 
> individual mail.
> Cheers,
> -Luke
>
>

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


[Tutor] Witch gui to choose for this script?

2006-10-27 Thread rolando

Well, I don't know if I can ask this question here, but never mind that :)

It´s like this, I created this python script that translates "human 
language" to Al-bhed language (it's a language from the game Final 
Fantasy 10, it's basicly a change in the letters for example A becomes W 
or B become H).


Since I like the script, and it's my first "program", I am now trying to 
create a GUI for it.


So what do you recommend?

I'm going to attach the script, just in case you want to see it.
#!/usr/bin/python
#V 0.4
import string

contador = "s" # Cria um contador que verifica que deve ou nao executar o script

while contador.lower() == "s" or contador.lower() == "sim": # Enquanto o valor for "s" ou "sim" o programa nao sai (converte a palavra em minusculas)

	print "Escolhe a lingua que queres traduzir. [p]ortugues ou [a]l-bhed"

	lingua = raw_input() # Escolhe a lingua
	
	if lingua.isspace() == True: # Se "lingua" e constituida por espacos em branco

		print "Tens de escrever alguma coisa ok?"

	if lingua == "": # Se lingua estiver vazia
		
		print "Tens de escrever alguma coisa ok?"

	if lingua.lower() == "a" or lingua.lower() == "al-bhed":
		lista = string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'epstiwknuvgclrybxhmdofzqajEPSTIWKNUVGCLRYBXHMDOFZQAJ') # Cria a lista de palavra e de que maneira vao ser trocadas

		print "Escreve a palavra que queres traduzir."

		palavra = raw_input() # Pede por uma palavra

		print "" + palavra.translate(lista) + "" # Imprime as palavras trocadas

	if lingua.lower() == "p" or lingua.lower() == "portugues":
		lista = string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ypltavkrezgmshubxncdijfqowYPLTAVkREZGMSHUBXNCDIJFQOW') # Cria a lista de palavra e de que maneira vao ser trocadas

		print "Escreve a palavra que queres traduzir."

		palavra = raw_input() # Pede por uma palavra

		print "" + palavra.translate(lista) + "" # Imprime as palavras trocadas
	

	print "Ainda tens mais alguma coisa para traduzir? [S/N]"
	
	contador = raw_input() # Modifica o contador que esta inicio do script

if contador.lower() == "n" or contador.lower() == "nao": # Se contador igual a "n" ou "nao" minusculos
	print "Entao adeus."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Witch gui to choose for this script?

2006-10-27 Thread rolando
If you're just going for a simple two-textbox, one button, 'type here, 
hit the button, and the translated text appears in the other box' 


Yeah, its something like that :D

Nothing to much fancy, it just that it can get kind of boring always 
going to the linux console to run the script.


I'm going to try the Boa Contructor.


>I'm going to attach the script, just in case you want to see it.
Good job on attaching the script!
I did want to see it.
I can't read the comments, though, but I think I got what it was doing.


Yeah, I kind of forgot to translate the comments.

Now I have attached the script with the translated comments.

Also any comments about the program are welcome.





Luke Paireepinart escreveu:

rolando wrote:
Well, I don't know if I can ask this question here, but never mind 
that :)


It´s like this, I created this python script that translates "human 
language" to Al-bhed language (it's a language from the game Final 
Fantasy 10, it's basicly a change in the letters for example A 
becomes W or B become H).


Since I like the script, and it's my first "program", I am now trying 
to create a GUI for it.


So what do you recommend?


I don't think something like this lends itself to a particular GUI set,
so the question I have to ask you is:
Do you feel lucky?

Just kidding.
It's really up to you on this one.
TKInter would probably be easier to learn, since it's your first program.
You could go the route of a GUI builder such as Boa Constructor, but 
I'd recommend that you learn
how to use a GUI toolkit before you use a builder, so that you know 
exactly what the builder is automating for you.


However, are you sure you want to jump into a GUI right now?
If you're just going for a simple two-textbox, one button, 'type here, 
hit the button, and the translated text appears in the other box'
type thing, and not something fancy, I guess it's about as simple of 
an introduction to a GUI toolkit as you can get, aside from a

label that says "Hello, World" or other such thing.
Just remember that it's important that you have a firm understanding 
of python's built-ins, and I guess Python in general,
before you start trying to use a big external package like a GUI 
toolkit, and a little up-front time learning the basics will help you 
a great deal in the long run.


>I'm going to attach the script, just in case you want to see it.
Good job on attaching the script!
I did want to see it.
I can't read the comments, though, but I think I got what it was doing.

Good luck in whatever you choose to do.
-Luke




#!/usr/bin/python
#V 0.4
import string

contador = "s" # Creates a counter that checks if it should run the script or not

while contador.lower() == "s" or contador.lower() == "sim": # While the counter is a lower s or sim (that's yes in portuguese) the program doesn't quit

	print "Escolhe a lingua que queres traduzir. [p]ortugues ou [a]l-bhed"

	lingua = raw_input() # Chosse the language to translate
	
	if lingua.isspace() == True: # If "lingua" is only white space

		print "Tens de escrever alguma coisa ok?"

	if lingua == "": # If "lingua" is empty
		
		print "Tens de escrever alguma coisa ok?"

	if lingua.lower() == "a" or lingua.lower() == "al-bhed":
		lista = string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'epstiwknuvgclrybxhmdofzqajEPSTIWKNUVGCLRYBXHMDOFZQAJ') # Creates the list of letter, and the way they are going to be changed

		print "Escreve a palavra que queres traduzir."

		palavra = raw_input() # Ask for a word

		print "" + palavra.translate(lista) + "" # Print the translated words

	if lingua.lower() == "p" or lingua.lower() == "portugues":
		lista = string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ypltavkrezgmshubxncdijfqowYPLTAVkREZGMSHUBXNCDIJFQOW') # Creates the list of letter, and the way they are going to be changed

		print "Escreve a palavra que queres traduzir."

		palavra = raw_input() # Ask for a word

		print "" + palavra.translate(lista) + "" # Print the translated words
	

	print "Ainda tens mais alguma coisa para traduzir? [S/N]"
	
	contador = raw_input() # Possibility of changing the counter in the begging of the script

if contador.lower() == "n" or contador.lower() == "nao": # If counter equals lower "n" or "nao" (that portugues for no) the program quits and prints the lower message
	print "Entao adeus."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2007-05-12 Thread Rolando Pereira

adam urbas escreveu:

Hi,I just started python today and I would like a few pointers, if you don't mind.  I tried 
using a tutorial, but was only able to get the correct results for the most basic problems.  
# Area calculation programprint “Welcome to the Area calculation program”print 
“–”print# Print out the menu:print “Please select a shape:”print “1  
Rectangle”print “2  Circle”# Get the user’s choice:shape = input(“> “)# Calculate the 
area:if shape == 1:height = input(“Please enter the height: “)width = input(“Please 
enter the width: “)area = height*widthprint “The area is”, areaelse:radius = 
input(“Please enter the radius: “)area = 3.14*(radius**2)print “The area is”, 
areaI've been trying to get this to work.  I was on a forum on Google and they said to 
put:input("press ENTER to continue")at the end.  I did, but it didn't work.  It 
runs the program but just shuts itself off when its done and i don't even get to select any 
of the option things that i'm s

upposed to be able to select.  It just turns on then back off and I don't even 
get to see anything.  Could someone help me out.ThanksAdam

_
Create the ultimate e-mail address book. Import your contacts to Windows Live 
Hotmail.
www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507




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


First, welcome to the world of Python. :D
Second. please give a title when you start a new thread on a mailing list.
Third, format your posts and code. Since Python uses indented code, it's 
kinda hard to read it when it's all in one line (Don't worry, I'll paste 
it indented in a file attached to this email :D )


Now for the code.

After arranging the code, the first thing I noticed were this characters “ ”

I tried running the code, and if gave me a error there, so I just 
replace then with " ", and voilá, the code worked :D . So the lesson 
here is always use either " " or ' ' in the code.


Oh, also another thing. Don't use input() to get the user input, because 
that command can run code and it may be evilly used. Always use 
raw_input() instead :D .


Anyway, I hope I helped you,


--
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
#!/usr/bin/python

# Area calculation program

print "Welcome to the Area calculation program"
print "–––––––––––––"
print

# Print out the menu:
print "Please select a shape:"
print "1  Rectangle"
print "2  Circle"

# Get the user’s choice:
shape = input("> ")

# Calculate the area:
if shape == 1:
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area = height*width
print "The area is", area
else:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] two input acceptions

2007-05-18 Thread Rolando Pereira
adam urbas escreveu:
> Thanks for the help.  I've made quite some progress since I first posted this 
> email.  I have a question though, what did you mean when you were talking 
> about the raw_input( )?  How can the regular input( ) be used evilly?  If you 
> could explain in depth, I would be very grateful.  I have a new question 
> related to my program area.py., I guess it's the same one as before.  When I 
> run the program and input the rectangle option, it asks me for a radius, 
> unless I input 1, instead of rectangle.  How do I program it to accept both 1 
> and rectangle?> Date: Sat, 12 May 2007 18:55:20 +0100> From: [EMAIL 
> PROTECTED]> To: [EMAIL PROTECTED]> CC: tutor@python.org> Subject: Re: [Tutor] 
> (no subject)> > adam urbas escreveu:> > Hi,I just started python today and I 
> would like a few pointers, if you don't mind.  I tried using a tutorial, but 
> was only able to get the correct results for the most basic problems.  # Area 
> calculation programprint “Welcome to the Area calculation program”print “––
–––”print# Print out the menu:print “Please select a shape:”print “1  
Rectangle”print “2  Circle”# Get the user’s choice:shape = input(“> “)# 
Calculate the area:if shape == 1:height = input(“Please enter the height: 
“)width = input(“Please enter the width: “)area = height*widthprint 
“The area is”, areaelse:radius = input(“Please enter the radius: “)area 
= 3.14*(radius**2)print “The area is”, areaI've been trying to get this to 
work.  I was on a forum on Google and they said to put:input("press ENTER to 
continue")at the end.  I did, but it didn't work.  It runs the program but just 
shuts itself off when its done and i don't even get to select any of the option 
things that i'm s> upposed to be able to select.  It just turns on then back 
off and I don't even get to see anything.  Could someone help me 
out.ThanksAdam> > 
_> > Create the 
ultimate e-mail address book. Import your cont
acts to Windows Live Hotmail.> > 
www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507>
 > > > > > 
> > > > 
___> > Tutor maillist  -  
Tutor@python.org> > http://mail.python.org/mailman/listinfo/tutor> > First, 
welcome to the world of Python. :D> Second. please give a title when you start 
a new thread on a mailing list.> Third, format your posts and code. Since 
Python uses indented code, it's > kinda hard to read it when it's all in one 
line (Don't worry, I'll paste > it indented in a file attached to this email :D 
)> > Now for the code.> > After arranging the code, the first thing I noticed 
were this characters “ ”> > I tried running the code, and if gave me a error 
there, so I just > replace then with " ", and voilá, the code worked :D . So 
the lesson > here is always use either " " or ' ' in the code.> > Oh, a
lso another thing. Don't use input() to get the user input, because > that 
command can run code and it may be evilly used. Always use > raw_input() 
instead :D .> > Anyway, I hope I helped you,> > > -- > 
_> ASCII ribbon campaign ( )>   - against HTML email  X>   & vCards 
/ \
> _
> Create the ultimate e-mail address book. Import your contacts to Windows Live 
> Hotmail.
> www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_impcont_0507

First of all, what email client are you using?
Because the text is getting all weird and difficult to read (it's all in 
one line, with no paragraphs and things like that).

Now, the thing about input() and why it's not a good policy to use is 
that, unlike raw_input(), what type in a input() is executed by Python 
(in raw_input() is stored as a string).

Example:

var = raw_input()
 >> list("LOL")

Now we have a variable called var which contains the string that says 
'list("LOL")'
You can confirm that by typing:
print var
 >> 'list("LOL")

There, no harm done. Now let's try the same thing using the input() command:

var = input()
 >> list("LOL")

Now let's type "print var" again as we did before.

print var
 >> ['L', 'O'. 'L']

Now what happened? Because you used the input() command, what you type 
was interpreted by Python, instead of being stored in a string and since 
the list() command is used to create a list, Python did just that. He 
created a list. Now, in this example, no harm was done. But image 
someone typing the command os.system("command to delete some file or run 
some file"). That would send a delete command to the terminal, or 
install some file (it could even be a virus).

Ok, it's a little harder to explain, but the thing you should is that 
usually raw_input() = GOOD, input() = BAD.




Now, I couldn't quite understand

Re: [Tutor] "#!/usr/bin/env python" vs "#!/usr/local/bin/python"

2007-06-14 Thread Rolando Pereira
[EMAIL PROTECTED] escreveu:
> Okay, I guess, people are missing points here. 
>  
> When do you
>  
> #!/usr/local/bin/python
> You are specifying the location to the python executable in your machine, 
> that rest of the script needs to be interpreted with.
> You are pointing to python is located at /usr/local/bin/python
>  
> Consider the possiblities that in a different machine, python may be 
> installed at /usr/bin/python or /bin/python in those cases, the above #! will 
> fail.
> For those cases, we get to call the env executable with argument which will 
> determine the arguments path by searching in the $PATH and use it correctly.
>  
> Thus,
> #/usr/bin/env python
> Will figure out the correct location of python ( /usr/bin/python or 
> /bin/python from $PATH) and make that as the interpreter for rest of the 
> script.
> - ( env is almost always located in /usr/bin/ so one need not worry what is 
> env is not present at /usr/bin)
>  
> Hope this helps.
>  
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

I've always used #!/usr/bin/python...

Perhaps I should use the "#!/usr/bin/env python" one.

-- 
_
ASCII ribbon campaign ( )
  - against HTML email  X
  & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Hi,every one

2007-06-22 Thread Rolando Pereira
Yang Yang escreveu:
> i am a newman for python world
> 
> i have some word want to ask
> 
> 
> 1.what is the best book for python study.
> 

I like Dive into Python.
( http://www.diveintopython.org/ )

> 2.what's is the better IDE for python
> 

That depends on what OS you are.

> 
> Thanks for all
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to Practice Python?(Linpeiheng)

2007-10-01 Thread Rolando Pereira
Alan Gauld wrote:
> There are no such things
> as standard solutions to programming problems, its not like
> doing math!

But usually there is "The Right Way".

I think...

-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The name of the module

2007-10-17 Thread Rolando Pereira
János Juhász wrote:
> Dear Tutors,
> 
> there was a thread some weeks ago about
> how can we find out 
> what is the name of the current module, 
> where the function was loaded from,
> where the function running from or so, 
> with some magic.
> 
> I can't find it in the archive.
> 
> May someone help me with some reference about it ?
> 
> Yours sincerely,
> __
> János Juhász
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

I can only find something back in February of this year.

http://mail.python.org/pipermail/tutor/2007-February/052914.html

Don't know if that's what you're after though.

-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getting filen basename without extension

2008-01-10 Thread Rolando Pereira
Timmie wrote:
> Hello,
> I would like to get the name of a file without it's extension/suffix.
> 
> What is the easiest and fastes way to get the basename
> of a file wihout extension?
> 
> What I found is this:
> import os
> myfile_name_with_path = 'path/to/my/testfile.txt'
> basename = os.path.basename(myfile_with_path)
> filename = os.path.splitext(basename)
> myfile_name_without_suffix = filename[0]
> 
> Can this be done with less code?
> 
> Thanks and kind regards,
> Timmie
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

I did something like this:

import os
path = 'path/to/file.ext'
filename = path.split(".")[0].split("/")[-1]
print filename
>>> file

The only problem I see is if the file has some "." character besides the
one before the extention.

-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Even More Converter!

2008-03-22 Thread Rolando Pereira
Kepala Pening wrote:
> import re
> 
> num = 123456789
> 
> print ','.join(re.findall("\d{3}", str(num)))
> 
> output:
> 123,456,789
> 
[snip]

The problem with that is that it cuts the digits in the end of the
number, if they can't form a 3 digit value.

Example:

import re
n = 1234

print ",".join(re.findall("\d{3}", str(n)))

Output: 123, instead of 1,234

I think the use of a function would be better.


def convert_num(num):
num = map(lambda x: int(x), str(num))
num.reverse()
k=0
tmp_number = []
for i in range(len(num)):
if k == 2 and i != range(len(num))[-1]:
tmp_number.append(num[i])
tmp_number.append(",")
k = 0
else:
tmp_number.append(num[i])
k += 1
num = map(lambda n: str(n), tmp_number)
num.reverse()
num = "".join(num)
return num


First it converts the number into a list in which each digit is a
separate member.

Then it reverses that list (because when we want to add the commas, we
start to count from the right and not from the left, that is, it's 1,234
and not 123,4).

Next a few loop variables (k and tmp_number), and we loop through the
"num" list, appending it's reversed digits into "tmp_number", except
when we have added 2 numbers without adding a comma, so we append the
next number AND a comma.

When the cycle ends, tmp_number is a list of ints with "," string
separating groups of 3 numbers.

In the end, it make "num" the same as "tmp_number", with all it's
members turned to strings (didn't knew that join() only worked with
strings in a list), reverse it (so it returns the same number that was
in the beginning, and joins everything into a string.

Example:

n = 1234
convert_num(n)

Output: 1,234

-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Even More Converter!

2008-03-22 Thread Rolando Pereira
Alan Gauld wrote:
> 
> If that is important you might need to investigate a locale specific 
> way of defining the seperator. I know Windows has hooks to get 
> it from the local settings but I'm not sure about *nix and I don't 
> know if Python has a generic way.
> 
> This might not matter to you in practice , but I thought I'd 
> mention it just in case...
> 

There is always the shell command "locale".

___

import subprocess

output = subprocess.Popen("locale | grep NUMERIC", shell=True, \
stdout=subprocess.PIPE)
print output.communicate()[0]

LC_NUMERIC="pt_PT.UTF-8" (In my case)

___

The problem with this is that it assumes the default shell is properly
  setted up (which may not be the case, for example this output happens
in Bash, but in ZSH it gives en_US.UTF-8), which may not be the case.

-- 
   _
ASCII ribbon campaign ( )
 - against HTML email  X
 & vCards / \
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor