[Tutor] How to perform variable assignment and

2009-10-02 Thread Didar Hossain
Hi,

I am currently learning Python and I was wondering is there a shorter
way to do the following -

import os

homedir = os.environ.get('HOME')

if homedir:
print "My home directory is %s" % homedir


I do this in Perl -

my $home;

if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; }

Can I do a similar shortcut statement like the above in Python?


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


Re: [Tutor] How to perform variable assignment and

2009-10-03 Thread Didar Hossain
On Sat, Oct 3, 2009 at 1:34 PM, Rich Lovely  wrote:
> 2009/10/3 wesley chun :
>> On Fri, Oct 2, 2009 at 11:14 PM, Oxymoron  wrote:
>>> Hello,
>>>
>>> On Sat, Oct 3, 2009 at 3:56 PM, Didar Hossain 
>>> wrote:
>>>>
>>>> homedir = os.environ.get('HOME')
>>>>
>>>> if homedir:
>>>>    print "My home directory is %s" % homedir
>>>>
>>>>
>>>> I do this in Perl -
>>>>
>>>> my $home;
>>>>
>>>> if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; }
>>>>
>>>> Can I do a similar shortcut statement like the above in Python?
>>>
>>> There are probably various syntactic tricks to achieve the same, however,
>>> the main reason you can't do it is that assignment in Python is a statement
>>> rather than an expression, i.e. it does not return a value that the if
>>> statement can evaluate.

Ok, this is what I understood as the difference between "expression"
and "statement":

statement => directive to do something
expression => check for truth value or "evaluatable" code

Am I correct?

>>
>> kamal is correct. you cannot do it in Python because assignments are
>> not expressions, and when they are, it leads to problems, i.e., code
>> readability, bugs, etc. Python fights hard to prevent those from
>> "interrupting your problem-solving," and there's a cost to it --
>> hopefully the benefits outweigh the minor costs.
>>
>> as far as your solution goes, it is one of the cleanest solution you
>> can come up with. however, there is a tiny bug: if the $HOME
>> environment variable is *not* set, you will get a KeyError exception.
>> one solution is to add a default value to your get() method call so
>> that it returns an object with a Boolean False value:
>>
>> import os
>>
>> homedir = os.environ.get('HOME', '') # or False or None

This is neat - didn't know I could do that! :-)

>>
>> if homedir:
>>   print "My home directory is %s" % homedir

>
> This message mentions, but skips over one of the differences in the
> mindsets of perl and python.
>
> Perl is designed to "Look Before You Leap", hence the if-statement in
> your example.  Python is designed with the mindset that "It's easier
> to ask forgivness than permission".  This is commonly known as LBYL
> vs. EAFP
>
> So whilst the perl would be
> if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; }
>
> the congruent Python would probably would be something like
>
> try:
>    home = os.environ['HOME']
> except KeyError:
>    home = None
> else:
>    print "My home directory is", home

This is nice, will take some getting used to.

> Admittedly, I don't know what the value of $home would be after
> executing the snippet above, but I'm assuming nil or null or whatever
> the perl equivalent is.

"undef" ;-)

> In a sort of summary:  LBYL means lots of if-statements: Is there a
> chance value X won't work in this function, if so, let's not try.
> EAFP means lots of exception handling: Let's try X in this function,
> and if it goes wrong, we'll deal with it then.

Hmmm, seems like I have a lot of unlearning to do. I have to do C as
part of my homework and use Perl for hacking up small scripts, so this
kind of tendency will be a little difficult to curb.

Thank you to all of you,
Didar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Checking for Python version

2009-10-06 Thread Didar Hossain
Hi,

I am using the following code to check for the Python version -

import os

t = os.sys.version_info[0:2]
if (t[0] + t[1]) < 6:
os.sys.exit("Need at least Python 2.4")
del t

This snippet is put at the beginning of the single script file before
the rest of the code.
I need to check for the minimum specific version because I am using
the "@staticmethod"
directive.

Is there a prettier way or is this fine?

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


Re: [Tutor] Checking for Python version

2009-10-07 Thread Didar Hossain
I like Kent's "try" method to explicitly look for the "staticmethod"
call - it is Pythony :-)

Todd's graceful handling idea is a good one - will keep that for future use.

Christian's input about my kludge failing with the 3.x series is a
good catch, I didn't think about that.

Thanks to all of you for the other observations.

Regards,

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


[Tutor] [OT] Secure coding guidelines

2009-10-10 Thread Didar Hossain
Hi,

This is a little off-topic, but, I though I might put this question in.

Since I am learning Python, I was wondering if there are any good
references on secure
coding practices. Books, guides or even any howtos would suffice.

Security seems to be almost always an after-thought rather than being
ingrained into
any course that I have come across including the ones that they have
in college degrees.

If this question is inappropriate for this list then please let me
know and accept my apologies
(EAFP) ;-)

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


Re: [Tutor] extracting informations (images and text) from a PDF andcreating a database from it

2009-12-29 Thread Didar Hossain
On Tue, Dec 29, 2009 at 3:21 PM, Shashwat Anand
 wrote:
> I used PDFMiner and I was pretty satisfied with the text portions. I
> retrieved all the text and was able to manipulate it according to my wish.
> However I failed on Image part. So Technically my question reduces to 'If
> there  a PDF document and some verbose text below them and the pattern is
> followed i.e. per page of PDF there will be one image and some texts
> following it, how can I retrieve both the images and the text without loss'
> ?

You can use `pdftohtml' [http://pdftohtml.sf.net]. It is available on Ubuntu.

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