Re: [Tutor] Shared web host and setting the python environment...

2011-03-05 Thread Izz ad-Din Ruhulessin
Hi,

I would go for appending sys.path. This is a simple way to do it, and
perhaps not even needed for each python file (only files which use the
modules that you have installed))

Another way to do it is adding the modules you need to sys.modules manually.

2011/3/5 Modulok 

> List,
>
> Background:
> I'm on a linux based shared web host. They cater to PHP, not python.
> (There's no third party python modules even installed!) So I installed
> a virtual python in my home directory, plus easy_install and a bunch
> of modules. Great!
>
> The Problem:
> Unfortunately, the web server uses the system python, not my virtual
> python, or my modules. (It does execute my scripts as my uid though.)
> I need modify the environment of every one of my python scripts,
> perhaps appending to sys.path, so that my third party modules in my
> home directory can be found. I have zero control over the web server
> process or its environment.
>
> Possible Solutions I thought of:
> One way to do this would be to edit 'sys.path' at the top of every
> python script. (Sounds ugly.) Another would be to import the 'user'
> module and go that route. (Fine, but it's depricated. What replaces
> it?) A third idea would be to re-direct all requests via mod_rewrite
> to a single python file or shell script, which sets the environment
> and calls my python scripts as a sub-process. (Slow, which is fine,
> but some security concerns here.)
>
> Given my situation, what's the best way to accomplish this? (Aside
> from a new hosting company or a virtual private server.)
>
> Any suggestions appreciated. Thanks!
> -Modulok-
> ___
> 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


Re: [Tutor] Help! (solution)

2011-03-05 Thread Alan Gauld


"michael scott"  wrote

...if something about my code could be better, please tell me,


OK, Here are some stylistic things...


def production_time():
   creation_time = 127
   time_till_rest = 18161


I'd move the constants out of the function to global level.
And it is convention to make constants all CAPS:

CREATION_TIME = 127

Also rather than arbitrary values show how these are arrived at:

NUMBER_TILL_REST = 143
TIME_TILL_REST = CREATION_TIME * NUMBER_TILL_REST



   items = raw_input("How many items will be produced?\n> ")
   item_time = int(items) * creation_time
   rest_times = item_time/time_till_rest
   print rest_times
   if rest_times > 0:
   total = item_time + (313 * rest_times) #313 is 5 min and 13 
secs in

second form


Rather than the "magic number" 313 I'd make it another constant.:

REST_TIME = (5 * 60) + 13   # in seconds


   else: total = item_time
   time = sec_to_standard(total)
   print "It would take %d days %d hours %d minutes and %d seconds 
to produce

that many items" %(time[0], time[1], time[2], time[3])


def sec_to_standard(seconds):
   day = 86400 #secs
   hour = 3600 #secs
   mins = 60#seconds
   creation_time = 127 #secs
   time_till_rest = 18161 #secs


More constants and some are duplicates.
Take them outside and you only have to change them in one place
so mainmtenance becomes easier. And the times get expressed as 
calculations:


MIN=60
HOUR = MINS * 60
DAY = HOUR * 24
REST_TIME=(5* MIN)+13


   days = 0
   hours = 0
   minutes = 0
   secs = 0


Some prefer the single line style of

days = hours = minutes = secs = 0


   if seconds > day:
   while seconds > day:
   print "doing days"
   seconds = seconds - day
   days += 1


This is a complicated way of doing modulo division!

days,seconds = divmod(seconds,DAY)


   if seconds > hour:
   while seconds > hour:
   print "doing hours"
   seconds = seconds - hour
   hours += 1


hours,seconds = divmod(seconds,HOUR)


   if hours >= 24:
   days += 1
   hours -= 24


This shouldn't be necessary because you already cut out the days


   if seconds > mins:
   while seconds > mins:
   print "doing minutes"
   seconds = seconds - mins
   minutes += 1


minutes,seconds = divmod(seconds,MIN)


   if minutes > 60:
   hours += 1
   minutes -= 60


Again it shoudn't be necessary but if it had been you would have
had to check the days again after increasing the hours...


   secs = seconds


You don't need this, just return seconds...


   return days, hours, minutes, secs

production_time()


Just some ideas.

--
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] Help - want to display a number with two decimal places

2011-03-05 Thread Alan Gauld


"Modulok"  wrote

However, to make all of your numbers line up nicely, regardless of 
how

long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice


Or, if your version of Python doesn't support string.format() (pre 
2.6?)

you can use \t characters to insert tabs and provide a field length
value in the format string and use - signs for string alignment.
If you need more you can do double formatting where you use
an initial format string to create the output values then insert
those into the final output string.

The new format() method is more powerful in these jinds of situation
(although sadly still not as powerful as COBOLs "picture" feature 
:-( )


So your strings might look like:


   # First prints the first argument (the 0th index) to 'format(),
   # left aligned '<', and 18 characters wide, whitespace padded.
   # Then prints the second argument, (the 1st index) right aligned,
   # also 18 characters wide, but the second argument is specified 
to

   # be a float 'f', that has a precision of 2 decimal places, '.2'.


print "%-18.0s\t%18.2f" %()

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


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-05 Thread Alan Gauld


"ranjan das"  wrote

The following code works fine in Python 2.6 but throws up an error 
in Python

2.7. Can anyone please say why?

import datetime
import calendar

while monday.weekday() != calendar.MONDAY:


where does 'monday' come from?
Can you send a minimal example that could actually run that shjows the 
error?

This is obviously just a code fragment.


Error:
Traceback (most recent call last):
 File "C:\Python27\Foursoft\calendar.py", line 33, in 
   while friday.weekday() != calendar.FRIDAY:
AttributeError: 'module' object has no attribute 'FRIDAY'


The obvious solution is that calendar has changed between 2.6 and 2.7
but looking on v3.1(I don;t have 2.7 installed) it looks the same 
there.

Certainly the day constants are still there.

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


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-05 Thread Steven D'Aprano

ranjan das wrote:

I ran the following code in python 2.6 and then in python 2.7 (using
calendar module) to manipulate dates and times

The following code works fine in Python 2.6 but throws up an error in Python
2.7. Can anyone please say why?


No it does not work fine at all. It generates a SyntaxError in every 
version of Python, including 2.6 and 2.7:


print next week
  ^
SyntaxError: invalid syntax


After fixing that error, I get:

NameError: name 'monday' is not defined


Please COPY and PASTE the ACTUAL code you run, do not retype it. Make 
sure that it actually does run before claiming it runs. Do not waste 
everybody's time with junk code that doesn't run, and them waste our 
time again with not one but *two* corrections, neither of which actually 
fix the broken code.





--
Steven

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


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-05 Thread Steven D'Aprano

Alan Gauld wrote:


"ranjan das"  wrote



Error:
Traceback (most recent call last):
 File "C:\Python27\Foursoft\calendar.py", line 33, in 
   while friday.weekday() != calendar.FRIDAY:
AttributeError: 'module' object has no attribute 'FRIDAY'


The obvious solution is that calendar has changed between 2.6 and 2.7
but looking on v3.1(I don;t have 2.7 installed) it looks the same there.
Certainly the day constants are still there.


What's "Foursoft"?

I would say that the standard library calendar module is being shadowed 
by another module with the same name.




--
Steven

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


[Tutor] Sorting a dictionary by a value when the values are tuples

2011-03-05 Thread ranjan das
This is a small example i created


from operator import itemgetter

temp={'4':(2,3), '2':(5,8)}

print temp.items()

new_temp=sorted(temp.items(), key=itemgetter(1)

print new_temp


I want to sort the dictionary by the value in the first element of the tuple
((2,3) and (5,8)) and then the second value.

How do i do it? by setting key=itemgetter(1), it seems to sort the dict by
the first value of the tuple. But I am not able to do it for the second
value.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help - want to display a number with two decimal places

2011-03-05 Thread Steven D'Aprano

Modulok wrote:


Notice that 'd' in your string substitution means integers, not
floats. Any decimal places will be truncated when using 'd'. For
floats use 'f' instead. You an also specify a precision, such as
'%.2f' shows two decimal places.

However, to make all of your numbers line up nicely, regardless of how
long they are, you need to look into advanced string formatting via
the builtin string method 'format()'. It takes a little practice to
understand and use, but the results are exactly what you want.


str.format only exists from Python 2.6, but in any case, it certainly 
isn't true that you "need" to look at format to make numbers line up 
nicely. The examples you give can all be written using string interpolation:



print "{0:<18} {1:>18.2f}".format("Country Sales Tax", countrySalesTax)
print "{0:<18} {1:>18.2f}".format("Purchase Price", purchasePrice)
# First prints the first argument (the 0th index) to 'format(),
# left aligned '<', and 18 characters wide, whitespace padded.
# Then prints the second argument, (the 1st index) right aligned,
# also 18 characters wide, but the second argument is specified to
# be a float 'f', that has a precision of 2 decimal places, '.2'.



>>> countrySalesTax = 11.25
>>> purchasePrice = 124577.35
>>> print "%-18s %18.2f" % ("Country Sales Tax", countrySalesTax); \
... print "%-18s %18.2f" % ("Purchase Price", purchasePrice)
Country Sales Tax   11.25
Purchase Price  124577.35


str.format can do some things better than % interpolation, but 
formatting strings to a width is not one of them. It's also a matter of 
opinion which is more cryptic:


"%-18s %18.2f"
"{0:<18} {1:>18.2f}"



--
Steven

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


Re: [Tutor] Sorting a dictionary by a value when the values are tuples

2011-03-05 Thread Steven D'Aprano

ranjan das wrote:

This is a small example i created


from operator import itemgetter
temp={'4':(2,3), '2':(5,8)}
print temp.items()
new_temp=sorted(temp.items(), key=itemgetter(1)
print new_temp


Another syntax error. Please ensure that you test your code before 
posting. In this case, it's easy to fix: you're missing a closing 
bracket in the call to sorted().



I want to sort the dictionary by the value in the first element of the tuple
((2,3) and (5,8)) and then the second value.


That would be the ordinary sort order of tuples.


How do i do it? by setting key=itemgetter(1), it seems to sort the dict by
the first value of the tuple. But I am not able to do it for the second
value.


You can't sort *dicts*, because they are unordered. However, you extract 
the key:value items from the dict into a list, which gives you a list of 
tuples:


>>> from operator import itemgetter
>>> temp={'4':(2,3), '2':(5,8)}
>>> print temp.items()
[('2', (5, 8)), ('4', (2, 3))]

You want to ignore the key part (the first item in the *outer* tuples, 
namely '2' and '4'), and just sort by the value part (the second item of 
the outer tuples, namely (2, 3) and (5, 8) -- note that these are 
themselves also tuples.


The way to sort them is using itemgetter(1), exactly as you tried:

>>> new_temp=sorted(temp.items(), key=itemgetter(1))
>>> print new_temp
[('4', (2, 3)), ('2', (5, 8))]

What makes you think that it did not work correctly? Here is a better 
example, showing that it works fine:


>>> temp = {'a':(2,3), 'b':(5,8), 'c':(5,1), 'd':(2,1)}
>>> temp.items()
[('a', (2, 3)), ('c', (5, 1)), ('b', (5, 8)), ('d', (2, 1))]
>>> sorted(temp.items(), key=itemgetter(1))
[('d', (2, 1)), ('a', (2, 3)), ('c', (5, 1)), ('b', (5, 8))]




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


Re: [Tutor] Error while using calendar module in Python 2.7

2011-03-05 Thread Alan Gauld


"Steven D'Aprano"  wrote


 File "C:\Python27\Foursoft\calendar.py", line 33, in 


What's "Foursoft"?

I would say that the standard library calendar module is being 
shadowed


Ah, good catch. I didn't notice the path was non standard.

Alan G 



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


[Tutor] pyton module index, windoes error 5

2011-03-05 Thread Travis Pierce

I am new to programming and am using Allen Downey's How to Think Like a 
Programmer, 2nd Edition Python edition to start learning.  I am having trouble 
viewing the pydoc graphic on a local host.  I know this is not necessarily a 
Python question, but I would appreciate any advice that can be provided.
 
I am using Windows 7
 
I am on chapter 10 and trying to graphically view the Python Index of Modules 
on local host 7464. 
 
When attempting to do this in the command line I receive a windows error.
 
Here is the end of the message I receive:
 
File "C:\Python26\Lib\pkgutil.py", line 211, in iter_modules for fn in 
os.listdir:
WindowsError: [Error 5] Access is denied:  '.\\Application Data/*.*'
 
I thought that I may not have root access to the folders in question, but I 
have gone through and given myself administrative access to all documents in 
the c:\python26 directory, and I am still receiving this error.
 
Thanks for any help that you can provide!   
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor