Re: [Tutor] help related to unicode using python

2013-03-20 Thread शंतनू
Reply inline.
On 21/03/13 12:18 AM, Steven D'Aprano wrote:
> On 20/03/13 22:38, nishitha reddy wrote:
>> Hi all
>> i'm working with unicode using python
>> i have some txt files in telugu i want to split all the lines of that
>> text files in to words of telugu
>> and i need to classify  all of them using some identifiers.can any one
>> send solution for that
>
>
> Probably not. I would be surprised if anyone here knows what Telugu is,
> or the rules for splitting Telugu text into words. The Natural Language
> Toolkit (NLTK) may be able to handle it.
>
> You could try doing the splitting and classifying yourself. If Telugu
> uses
> space-delimited words like English, you can do it easily:
>
> data = u"ఏఐఒ ఓఔక ఞతణథ"
> words = data.split()
Unicode characters for telugu:
http://en.wikipedia.org/wiki/Telugu_alphabet#Unicode


On python 3.x,

>>> import re
>>> a='ఏఐఒ ఓఔక ఞతణథ'
>>> print(a)
ఏఐఒ ఓఔక ఞతణథ
>>> re.split('[^\u0c01-\u0c7f]', a)
['ఏఐఒ', 'ఓఔక', 'ఞతణథ']

Similar logic can be used for any other Indic script.

HTH.

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


Re: [Tutor] Calculate hours

2013-01-25 Thread शंतनू
Reply inline.

On 23/01/13 8:22 AM, anthonym wrote:
> Hello All,
>
> I originally wrote this program to calculate and print the employee
> with the most hours worked in a week.  I would now like to change this
> to calculate and print the hours for all 8 employees in ascending order.
>
> The employees are named employee 0 - 8
>
> Any ideas?
>
> Thanks,
> Tony
>
> Code below:
>
>
>
> # Create table of hours worked
>
> matrix = [
> [2, 4, 3, 4, 5, 8, 8],
> [7, 3, 4, 3, 3, 4, 4],
> [3, 3, 4, 3, 3, 2, 2],
> [9, 3, 4, 7, 3, 4, 1],
> [3, 5, 4, 3, 6, 3, 8],
> [3, 4, 4, 6, 3, 4, 4],
> [3, 7, 4, 8, 3, 8, 4],
> [6, 3, 5, 9, 2, 7, 9]]
>


===
s = [sum(x) for x in matrix]
for q in sorted([(x,y) for x,y in enumerate(s)],
key=operator.itemgetter(1)):
print("Employee %d has worked %d hours" % (q[0], q[1]))
===

Output:

Employee 2 has worked 20 hours
Employee 1 has worked 28 hours
Employee 5 has worked 28 hours
Employee 3 has worked 31 hours
Employee 4 has worked 32 hours
Employee 0 has worked 34 hours
Employee 6 has worked 37 hours
Employee 7 has worked 41 hours


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


Re: [Tutor] need help with python for counter

2012-12-18 Thread शंतनू

On 19/12/12 5:12 PM, Mitya Sirenef wrote:
> You can also do:
>
> count = sum(i==digit for i in number)
>
> I think this is very clear and short..

Another...

import re
count = len(re.findall(digit, number))

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


Re: [Tutor] ssh connection

2012-12-18 Thread शंतनू

On 18/12/12 7:51 PM, Alan Gauld wrote:
> On 18/12/12 08:03, Ufuk Eskici wrote:
>> I have Putty and I it runs from CMD successfully.
>>
>> *C:\Users\eufuesk>*"c:\Program Files\Putty\putty.exe" -ssh
>> [username]@10.0.0.1 <http://10.0.0.1>
>>   -pw [password]
>>
>> But I dont know how to send this command from Pyhton. :(
>
> Have you read the subprocess module documentation?
> It provides many examples of running commands.
>
> The raw Popen class can be a bit intimidating but there are wrapper
> functions that make it easier for simple applications.
>

You may also like to have a look at 'commands' module, especially when
no user input is required.

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


Re: [Tutor] reading web page with BeautifulSoup

2012-12-12 Thread शंतनू

On 13/12/12 12:47 PM, Ed Owens wrote:
> >>> from urllib2 import urlopen
> >>> page = urlopen('w1.weather.gov/obhistory/KDCA.html')
> Traceback (most recent call last):
>   File "", line 1, in 
>   File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
> line 126, in urlopen
> return _opener.open(url, data, timeout)
>   File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
> line 386, in open
> protocol = req.get_type()
>   File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py",
> line 248, in get_type
> raise ValueError, "unknown url type: %s" % self.__original
> ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html
> >>>
>
> Can anyone see what I'm doing wrong here?  I have bs4 and urllib2
> imported, and get the above error when trying to read that page.  I
> can copy the url from the error message into my browser and get the page.

You may try the URL with 'http://' or 'https://' instead of 'w1.'.

HTH.

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


Re: [Tutor] Flatten a list in tuples and remove doubles

2012-07-19 Thread शंतनू
You may use 'set'.

e.g.

===
>>> x
[(1, 2, 3), (1, 1), (2, 2), (1, 1), (2, 2)]
>>> set(x)
set([(2, 2), (1, 1), (1, 2, 3)])
===

On 19-Jul-2012, at 11:03 PM, PyProg PyProg wrote:

> Hi all,
> 
> I would get a new list as:
> 
> [(0, '3eA', 'Dupont', 'Juliette', '11.0/10.0', '4.0/5.0', '17.5/30.0',
> '3.0/5.0', '4.5/10.0', '35.5/60.0'), (1, '3eA', 'Pop', 'Iggy',
> '12.0/10.0', '3.5/5.0', '11.5/30.0', '4.0/5.0', '5.5/10.0',
> '7.5/10.0', '40.5/60.0')]
> 
> ... from this one:
> 
> [(0, '3eA', 'Dupont', 'Juliette', 0, 11.0, 10.0), (0, '3eA', 'Dupont',
> 'Juliette', 1, 4.0, 5.0), (0, '3eA', 'Dupont', 'Juliette', 2, 17.5,
> 30.0), (0, '3eA', 'Dupont', 'Juliette', 3, 3.0, 5.0), (0, '3eA',
> 'Dupont', 'Juliette', 4, 4.5, 10.0), (0, '3eA', 'Dupont', 'Juliette',
> 5, 35.5, 60.0), (1, '3eA', 'Pop', 'Iggy', 0, 12.0, 10.0), (1, '3eA',
> 'Pop', 'Iggy', 1, 3.5, 5.0), (1, '3eA', 'Pop', 'Iggy', 2, 11.5, 30.0),
> (1, '3eA', 'Pop', 'Iggy', 3, 4.0, 5.0), (1, '3eA', 'Pop', 'Iggy', 4,
> 5.5, 10.0), (1, '3eA', 'Pop', 'Iggy', 5, 40.5, 60.0)]
> 
> How to make that ? I'm looking for but for now I can't do it.
> 
> Thanks in advance.
> 
> a+
> 
> -- 
> http://ekd.tuxfamily.org
> http://ekdm.wordpress.com
> http://glouk.legtux.org/guiescputil
> http://lcs.dunois.clg14.ac-caen.fr/~alama/blog
> http://lprod.org/wiki/doku.php/video:encodage:avchd_converter
> ___
> 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] summary stats grouped by month year

2012-05-08 Thread शंतनू
On Tue, May 8, 2012 at 12:46 PM, शंतनू  wrote:
> Python generally comes with support for sqlite3.
>
> you can store your date in to sqlite db and then try running sql query
> for finding of the details.

data not date.

>
> select year, sum(MeanRain) where year='1972' group by year;
> select month, sum(MeanRain) where month='Jan' group by month;
>
> ** Not sure regarding the exact sql query. sum function and 'group by'
> is important.
>
> On Tue, May 8, 2012 at 11:37 AM, questions anon
>  wrote:
>> I would like to calculate summary statistics of rainfall based on year and
>> month.
>> I have the data in a text file (although could put in any format if it
>> helps) extending over approx 40 years:
>> YEAR MONTH    MeanRain
>> 1972 Jan    12.7083199
>> 1972 Feb    14.17007142
>> 1972 Mar    14.5659302
>> 1972 Apr    1.508517302
>> 1972 May    2.780009889
>> 1972 Jun    1.609619287
>> 1972 Jul    0.138150181
>> 1972 Aug    0.214346148
>> 1972 Sep    1.322102228
>>
>> I would like to be able to calculate the total rain annually:
>>
>> YEAR   Annualrainfall
>> 1972    400
>> 1973    300
>> 1974    350
>> 
>> 2011 400
>>
>> and also the monthly mean rainfall for all years:
>>
>> YEAR  MonthlyMeanRain
>> Jan  13
>> Feb  15
>> Mar   8
>> .
>> Dec   13
>>
>>
>> Is this something I can easily do?
>> I have started by simply importing the text file but data is not represented
>> as time so that is probably my first problem and then I am not sure how to
>> group them by month/year.
>>
>> textfile=r"textfile.txt"
>> f=np.genfromtxt(textfile,skip_header=1)
>>
>> Any feedback will be greatly appreciated.
>>
>>
>> ___
>> 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] summary stats grouped by month year

2012-05-08 Thread शंतनू
Python generally comes with support for sqlite3.

you can store your date in to sqlite db and then try running sql query
for finding of the details.

select year, sum(MeanRain) where year='1972' group by year;
select month, sum(MeanRain) where month='Jan' group by month;

** Not sure regarding the exact sql query. sum function and 'group by'
is important.

On Tue, May 8, 2012 at 11:37 AM, questions anon
 wrote:
> I would like to calculate summary statistics of rainfall based on year and
> month.
> I have the data in a text file (although could put in any format if it
> helps) extending over approx 40 years:
> YEAR MONTH    MeanRain
> 1972 Jan    12.7083199
> 1972 Feb    14.17007142
> 1972 Mar    14.5659302
> 1972 Apr    1.508517302
> 1972 May    2.780009889
> 1972 Jun    1.609619287
> 1972 Jul    0.138150181
> 1972 Aug    0.214346148
> 1972 Sep    1.322102228
>
> I would like to be able to calculate the total rain annually:
>
> YEAR   Annualrainfall
> 1972    400
> 1973    300
> 1974    350
> 
> 2011 400
>
> and also the monthly mean rainfall for all years:
>
> YEAR  MonthlyMeanRain
> Jan  13
> Feb  15
> Mar   8
> .
> Dec   13
>
>
> Is this something I can easily do?
> I have started by simply importing the text file but data is not represented
> as time so that is probably my first problem and then I am not sure how to
> group them by month/year.
>
> textfile=r"textfile.txt"
> f=np.genfromtxt(textfile,skip_header=1)
>
> Any feedback will be greatly appreciated.
>
>
> ___
> 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] write list to columns

2012-04-11 Thread शंतनू

On 12-Apr-2012, at 8:01 AM, questions anon wrote:

> I am trying to simply write a list of values to txt file in one column.
> I would then like to add another list in a second column.
> Somehow they only appear as one long row. 
> Any feedback will be greatly appreciated. 
> 
> print "rain max values", rmax
> print "yearmonth", month year

'print' adds newline automatically.
More info:
http://docs.python.org/reference/simple_stmts.html#print

> OutputFolder=r"E:/test_out/"
> myfile=open(OutputFolder+'test.txt','w')
> myfile.write(str(monthyear))
> myfile.write(str(rmax))
> myfile.close()

'write' does not add newline. You need to add '\n' where you want new line.


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


Re: [Tutor] how to use int and split() simultaneously

2011-12-08 Thread शंतनू

On 08-Dec-2011, at 7:13 PM, Dario Lopez-Kästen wrote:

> oh, yes,  no top posting. See below.
> 
> On Thu, Dec 8, 2011 at 1:33 PM, surya k  wrote:
> 
> This is something I am trying to do..
> Say, we are entering a string "1 2 3 4 5".so, I want to assign the numbers 
> directly as numbers. how can I do it?
> I could put that numbers as string but not as number..
> strNum = raw_input('enter:').split()
> I can convert the list into numbers by doing this...
> for i in range(len(strNum)):   strNum[i] = int(strNum[i]).
> but I feel, its a long process. How can I do it in the shortest possible way??
> 
> Using a list comprehension is one example of a compact way of doing it:
> 
> strNum = raw_input("enter numbers, separated by space: ").split()
> strNum = [int(x) for x in strNum]
>  
> You would have to assume that the entire string consists of tokens that can 
> be type cast into integers. If not you get an error:
> 
> >>> strNum = raw_input("enter numbers, separated by space: ").split()
> enter numbers, separated by space: 1 2 3 4 5 66 asd
> >>> strNum = [int(x) for x in strNum]
> Traceback (most recent call last):
>   File "", line 1, in ?
> ValueError: invalid literal for int(): asd
> 
> In that case a for loop with a try-except would better:
> 
> strNum = raw_input("enter numbers, separated by space: ").split()
> num_list = []
> 
> for token in strNum:
> try:
> num_list.append(int(token))
> except ValueError:
> # Do nothing, fetch the next token
> continue
> 
> print repr(num_list)
> 
> This code gives this result:
> 
> enter numbers, separated by space: 1 2 3 4 5 66 
> [1, 2, 3, 4, 5, 66]
> 
> enter numbers, separated by space: 1 2 3 4 5 66 asd 77
> [1, 2, 3, 4, 5, 66, 77]
> 
> Strictly speaking the "continue" in the except clause is not necessary in 
> this simple example.
> 
> Hope this helps!
> 
> /dario
> 

Using re module:

===
import re
strNum = raw_input("enter numbers, separated by space: ")
if re.search('[^\d ]', strNum):
print('Invalid input')
else:
data = [int(x) for x in strNum.split()]
print(data)
===___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] beginner here

2011-12-07 Thread शंतनू
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1


On 08-Dec-2011, at 6:47 AM, Do youknow who wrote:

> Im trying to write this program where i make it flip a coin 100 times then 
> tells me the number of heads and tails it came up with.
>  
> this is what I got but it does not run
>  
> # Coin Flip
> # Demonstrating the While loop
> import random
> print("I will flip a coin 100 times and tell you")
> print(", how many heads and how many tails I got.")
> coin_rolls = random.randint(1,2)
> heads = 0
> tails = 0
> rolls = 0
>  
> if rolls <= 100:
> rolls += 1
> 
> elif coin_rolls == 1:
> heads += 1
> elif coin_rolls == 2:
> tails += 1
> else:
> print("error")
> print("There was ", heads, "rolls for heads,")
> print("\nand there was ", tails, " rolls for tails.")
> input("\n\nPress the enter key to exit.")
> 
> I end up with 0 rolls for heads and 0 rolls for tails...I have made attempts 
> to put the
> "coin_rolls = random.randint(1,2)" within the loops but only end up with 
> errors
> what wrong with my code?


# head(h) --- even number
# tail(t) --- odd number
# roll(r) --- number of rolls

===
import random
r = 100
h = len([1 for x in range(r) if random.randint(1,1000) % 2])
t = r - h
print('There were %d rolls for head and %d rolls for tail.' % (h, t))
===

Little more pythonic way...

- -- 
शंतनू
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (Darwin)

iQEcBAEBAgAGBQJO4EHrAAoJEJYe6XdHCE0VlvQH/3hhvv4PL75EQe6wyVlUOuSw
fJiH8IMqm4BGUsgLOUhGyRETlDD3poI3Ae02zJN0K2VpGrNsqRq4VdN9GNBfHtoC
C90eM1dCvyNHWic4Lbo+9UQGDUFXAngeDNnpY7LjNEvjpyVGsog2ptiyIauoY8zy
ySun1rvcv+uUChd6+P/IEBppX6cf9ijgWt/zl8B0aCsJleCtOuh4WvTY4yX4upss
hQaCDta0L6Tog1SF9PdevJgFENTVK36DEXU43xzqlEYyNLGUBTXUd2DeeKes8GJd
kR553QTizOYDwWRgA7ZiJ99zr+oOt1s0wkpb01A8geXXDJo6sCfxbafAmizY18w=
=uCdb
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 6 random numbers

2011-10-16 Thread शंतनू
On 17-Oct-2011, at 1:13 AM, ADRIAN KELLY wrote:

> hello all,
> anyone know how i would go about printing 6 random numbers, i know i could 
> copy and paste 6 times (which would work) but i was thinking about a while 
> loop, ie. while lottery_numbers.count is <7. 
> Is it possible to code this? is it possible to count random variables? i am 
> trying to keep the program as simple as possible, cheers
> 
> any help would be welcome, 
> 
> import random
> lottery_numbers=random.randrange(1,42)
> print lottery_numbers
>   

Following example may be useful:
x = [random.randrange(1, 1+random.randrange(42) for _ in range(100)]

Useful read: 
http://docs.python.org/tutorial/datastructures.html

-- 
shantanoo
http://xkcd.com/221/

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


Re: [Tutor] Finding the differences between two lists

2011-08-25 Thread शंतनू
You may try using set instead of list.

>>> verifiedList = {[1,2,3}
>>> scanResults = {1,2,3,4,5}
>>> badMacs = scanResults - verifiedList
>>> badMacs
set([4, 5])
>>> verifiedList = {1,2,3,7,8,9}
>>> badMacs = scanResults - verifiedList
>>> badMacs
set([4, 5])

-- 
shantanoo
On 26-Aug-2011, at 12:26 AM, Justin Wendl wrote:

> Hello,
> 
> Bare with me, as I am new to Python and a beginner programmer.
> I am trying to compare two lists (not of the same length), and create a new 
> list of items that are -not- found in both lists.
> 
> Scenario: I have a list of MAC addresses that are known and good, and am 
> comparing it to a list of MACs found in a scan.  I want to weed out the those 
> which are unknown.  I am using IDLE (Python 2.7) on Windows, and all files 
> are in the same directory.
> 
> Code:
> 
> scanResults = open('scanResults.txt', 'r')
> verifiedList = open('verifiedList.txt', 'r')
> badMacs = []
> 
> for mac in scanResults:
>   if mac not in verifiedList:
> print mac
> badMacs.append(mac)
>   else:
> break
> 
> 
> When I print 'badMacs', the results come up empty:
> []
> 
> 
> Any help would be greatly appreciated!
> 
> Regards,
> Justin
> ___
> 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] Running Existing Python

2011-02-26 Thread शंतनू

On 27-Feb-2011, at 2:40 AM, Justin Bonnell wrote:

> On Feb 26, 2011, at 4:49 AM, Dave Angel wrote:
>> 3) You don't tell us where the hello.py file actually is.  Presumably it's 
>> not in the current directory when you run that.  Two cures for that:  either 
>> specify its real location,
>> python   ~/mysources/hello.py
> 
> --This is the location of the file:
> 
>   /jwbonnell/bin/Python 2.7/Extras/Demo/tkinter/guido/hello.py

You need to escape space.

Try

/jwbonnell/bin/Python\ 2.7/Extras/Demo/tkinter/guido/hello.py


There is '\' before space.

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


Re: [Tutor] multi-threaded/parallel processing - local tutor

2011-01-22 Thread शंतनू
You may find following useful.

2.6+ --- http://docs.python.org/library/multiprocessing.html
3.x --- http://docs.python.org/dev/library/multiprocessing.html


On 23-Jan-2011, at 11:46 AM, bruce wrote:

> Hi.
> 
> I'm working on a project that uses python to spawn/create multiple
> threads, to run parallel processes to fetch data from websites. I'm
> looking to (if possible) go over this in person with someone in the
> San Fran area. Lunch/beer/oj can be on me!!
> 
> It's a little too complex to try to describe here, and pasting the
> code/apps wouldn't do any good without an associated conversation.
> 
> So, if you're in the Bay area, and you're up to some in person
> tutoring, let me know.
> 
> Thanks guys for this list!!
> ___
> 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] end parameter in 2.7.1?

2011-01-15 Thread शंतनू


Python 2.7.1 (r271:86832, Jan 13 2011, 22:08:21) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
  File "", line 1
print('hello', end='')
  ^
SyntaxError: invalid syntax
>>> from __future__ import print_function
>>> print('hello', end='')
hello>>> 



HTH.

On 16-Jan-2011, at 2:04 AM, Karim wrote:

> 
> IN 3.1 for print():
> 
> >>> print('hello', end =' ')
> hello>>>
> 
> It suppress the newline character.
> 
> 
> On 01/15/2011 07:05 PM, Emile van Sebille wrote:
>> On 1/15/2011 9:35 AM Bill DeBroglie said...
>>> Twice in two days...!
>>> 
>>> Using Mac OS X 10.5.8 and Python 2.7.1 but am following a book which is
>>> using Python 3.1. The author uses the end parameter
>> 
>> What's an 'end' parameter?
>> 
>> Emile
>> 
>>> in numerous programs
>>> but this doesn't seem to translate to 2.7. Any advice as to how I can
>>> specify the final string of the print function?
>>> 
>>> Thank you,
>>> bdb
>>> ___
>>> 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 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] Choice of Python

2010-12-28 Thread शंतनू

On 28-Dec-2010, at 10:14 PM, Wayne Werner wrote:

> On Tue, Dec 28, 2010 at 10:05 AM, Brett Ritter  wrote:
>  (though I have to constantly reteach myself not to
> use semicolons :) ). 
> 
> Technically speaking, you *can* use semicolons in Python:
> 
> if 3 == int('3'):
>print('Cool');
> 
> works the same sans semicolon.

And so does following...

>>> if 3 == int('3'):
... print 'hello';print 'world'
... 
hello
world
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating a python file

2010-12-14 Thread शंतनू
On Tue, Dec 14, 2010 at 14:18, C.T. Matsumoto wrote:

> Hello,
>
> Is it possible to create files containing python code in the same sort of
> way that you can generate text files.
>
> A simple example perhaps could be a persistent dictionary. Keys and values
> are written to the dictionary in a file, that can be imported later.
>

http://docs.python.org/library/pickle.html is your friend.

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


Re: [Tutor] permutations?

2010-12-02 Thread शंतनू
Following link could be useful:

http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python

On Thu, Dec 2, 2010 at 04:15, Alex Hall  wrote:

> Hi all,
> I am wondering if there is a python package that will find
> permutations? For example, if I have (1, 2, 3), the possibilities I
> want are:
> 12
> 13
> 23
> 123
> 132
> 231
>
> Order does not matter; 21 is the same as 12, but no numbers can
> repeat. If no package exists, does someone have a hint as to how to
> get a function to do this? The one I have right now will not find 132
> or 231, nor will it find 13. TIA.
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehg...@gmail.com; http://www.facebook.com/mehgcap
> ___
> 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] querying the name of a calling python file

2010-11-05 Thread शंतनू

On 05-Nov-2010, at 4:45 PM, Garry Willgoose wrote:

> For a while now I have been using the command below in a python file to 
> determine the name of the python file that I use to launch an application 
> (i.e.IF I go 'python junk.py' I want to get 'junk.py'). The command I have 
> use that I came across somewhere some time ago was
> 
> sys.modules['__main__'].__dict__['__file__']
> 
> Now this works fine for the standard python interpretter but when I use 
> ipython (I'm using the enthought distribution 6.1) this command returns
> 
> /Library/Frameworks/Python.framework/Versions/6.1/lib/python2.6/site-packages/IPython/FakeModule.py
> 
> I discovered this when  I was tracking down inconsistencies/errors between 
> the matplotlib in the enthought distribution depending on whether you use 
> python or ipython. My question is this: Is the way I'm getting the file name 
> the recommended way or did I pick up a bit of flaky advice? 
> 
> I'm puzzled by the matplot lib errors (the y axes on imgshow() show problems 
> in python but appear to 1st order to be OK in ipython) ... but I'll pursue 
> that at the Matplotlib, enthought and ipython forums once I've been able to 
> run a broader cross-section of my codes. 
> 

Try the following code:

===
#!/usr/bin/env python
import sys
print sys.argv
===

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


Re: [Tutor] possible to run a python script from non-cgi?

2010-10-30 Thread शंतनू
Hi Tim,
Reply inline.

On 31-Oct-2010, at 1:02 AM, Tim Johnson wrote:

> FYI: I am working in a linux environment with python 2.6.5
> am an experienced web developer with 8 years in python, but
> :) I have never tried this trick before:
> 
> I note that with the right .htaccess file, I can run a php file,
> from a non-cgi location.
> Example: On my machine, my wwwroot is at /home/http/, I have
> /home/http/php/test/index.php and I have run index.php as
> http://localhost/php/test/ (again with the correct .hataccess).
> 
> Is it possible to run a python script this way?

Have not tried it, but should be possible. 

Following link could be helpful.
http://docs.python.org/library/cgi.html

>From http://en.wikipedia.org/wiki/Common_Gateway_Interface

===
>From the Web server's point of view, certain locators, e.g. 
>http://www.example.com/wiki.cgi, are defined as corresponding to a program to 
>execute via CGI. When a request for the URL is received, the corresponding 
>program is executed.

Web servers often have a cgi-bin/ directory at the base of their directory tree 
to hold executable files called with CGI.
===

HTH.

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


Re: [Tutor] Writing Scripts.

2010-10-20 Thread शंतनू
Save the file. e.g. my_test_prog.py. Open 'terminal'. Run following command

'python my_test_prog.py'

On my 10.6.4 version of Mac OSX, I get following version of python.
===
$ /usr/bin/python
Python 2.6.1 (r261:67515, Dec 17 2009, 00:59:15) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
===


On 20-Oct-2010, at 9:43 PM, Autumn Cutter wrote:

> I just started learning Python last night and I'm a little stuck on how to 
> write a Python script. 
> I'm using Python 2.5.0 on a Mac OS X 10.6.4. I've tried using programs like 
> Idle, Applescript and Textmate but I'm still unable to write a script and run 
> it successfully. I'm wondering if I'm using the write program or if I 
> understand correctly how to write and run scripts. Help! :(
> 
> ___
> 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] decorators

2010-07-23 Thread शंतनू

On Friday 23 July 2010 11:53 PM, Mary Morris wrote:
I'm trying to compile a list of decorators from the source code at my 
office.

I did this by doing a

candidate_line.find("@")


How about using something like

candidate_line.strip.startswith('@') and calculate_line.find('.') == -1

There are few more cases where above may fail. In that case you may want 
to use re module instead.

e.g.
@test_decorator # this is test comment.



because all of our decorators start with the @ symbol.  The problem 
I'm having is that the email addresses that are included in the 
comments are getting included in the list that is getting returned.
I was thinking I could do a candidate_line.find(".com") to set the 
email addresses apart, but how do I tell the computer to not include 
the lines it finds with ".com" in them in the list?


The part of my code that I'm hoping to include this in looks like this:



#parse out the names of the decorators from those lines
return_decorators= []
for ele in subset_lines:
candidate_line, line_number = ele
candidate_line = candidate_line.strip()
i = candidate_line.find("(")
j = candidate_line.find("#")
#if () is in the last spot
if i == -1:
#if () is in the last spot and the decorator 
is in a comment

if j == 0:
#get rid of ( and #
candidate_line = candidate_line[2:i]
#if () is in the last spot and the decorator 
is not in a comment

elif j != 0:
candidate_line = candidate_line[1:i]
#if there are not ()'s in the last spot
elif i != -1:
#if there are not ()'s, but the decorator is 
in a comment

if j == 0:
candidate_line = candidate_line[2:]
#if there are not ()'s and the decorator isn't 
in a comment

elif j != 0:
candidate_line = candidate_line[1:]
 elif candidate_line.find(".com"):
candidate_line != candidate_line
return_decorators.append((line_number, candidate_line))
return return_decorators
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help required to count no of lines that are until 1000 characters

2010-05-11 Thread शंतनू

On 12-May-2010, at 12:32 AM, spir ☣ wrote:

> On Tue, 11 May 2010 11:00:20 -0700
> ramya natarajan  wrote:
> 
>> Hello,
>> 
>> I am very  beginner to programming, I  got task to Write a loop that reads
>> each line of a file and counts the number of lines that are read until the
>> total length of the lines is 1,000 characters. I have to read lines from
>> files exactly upto 1000 characters.
>> 
>> Here is my code:
>> I created file under /tmp/new.txt  which has 100 lines and 2700 characters
>> , I wrote code will read exactly 1000 characters and count lines upto those
>> characters.But the problem is its reading entire line and not stopping
>> excatly in 1000 characters. Can some one help what mistake i am doing here?.
>> 
>>   log = open('/tmp/new.txt','r')
>>   lines,char = 0,0
>>   for line in log.readlines():
>>while char < 1000 :
>>for ch in line :
>> char += len(ch)
>>lines += 1
>>  print char , lines
>>  1026 , 38    Its counting entire line  instead of character upto 1000
>> -- can some one point out what mistake am i doing here , where its not
>> stopping at 1000 . I am reading only char by car
>> 
>> My new.txt -- cotains content like
>> this is my new number\n
>> 
>> Can some one please help. I spent hours and hours to find issue but i am not
>> able to figure out, Any help would be greatly appreciated.
>> Thank you
>> Ramya
> 
> Either you read line per line, but then you cannot stop exactly at the 1000th 
> character; or you traverse the text char per char, but this is a bit picky.
> I would read line per line, and when count >= 1000, read chars inside current 
> line to get to the 1000th, if needed.
> (Your specification does not state this, but your disappointment seems to be 
> about that issue ;-)
> 
> Denis

You can try read instead of readlines.
Something like...
print 'Number of lines till 1000th character:', 
len(open('/tmp/new.txt','r').read(1000).split('\n'))

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


Re: [Tutor] Remote access from Windows PC to a Linux box

2010-03-30 Thread शंतनू
You can have a look @ paramiko library.

http://www.lag.net/paramiko/


On 30-Mar-2010, at 9:59 PM, Mike Baker wrote:

> Hi,
>  
> I'm trying to connect to a Linux box from my Windows machine and execute a 
> series of commands - (ls, pwd, cat 'somefile', etc...).   I'm using Putty to 
> do the ssh and have set up with Putty's Pagent agent to allow me to enter a 
> passphrase once per session to handle security keys between the two boxes 
> (so, no passwords needed for my remote scripts).
>  
> I have code that will give me a remote prompt on the Linux machine where I 
> can manually enter commands. This works great, but I want a script to always 
> execute the same series of commands without having to do so manually.   I 
> also have code that will execute a single command like cat a file and write 
> the ouput to a new file. However, when I try to use the communicate object in 
> subprocess, my window hangs. 
>  
> Here is my working code:
> # module name data_collect.y
> #
> import subprocess
>  
> def simp_tst0(s_name):
> # Opens a remote connection to "s_name and gives a prompt.
> # Works great for executing linux commands.
> # Does not exit gracefully when you type exit. The python
> # prompt hangs when it gets to the r.communicate command
> # 
> cmmnd_0="C:\\Progra~1\\putty\\plink %s" % s_name
> r = subprocess.Popen("%s" % cmmnd_0,shell=False)
> (r_stdout, r_stderr) = r.communicate("dir")
> #status=r.poll() #Locks up if you try to poll here
> print r_stdout
> return r
>  
> def cat_remote(s_name, file2cat):
> # This simple test file opens a remote connection to "s_name", does a cat 
> on
> # file "file2cat" and writes the cat to an output file (out2.txt). 
> cmmnd_2="C:\\Progra~1\\putty\\plink %s cat %s" % (s_name, file2cat)
> q = subprocess.Popen("%s" % cmmnd_2, stdout=open('out2.txt','w'))
>  
> 
> def simp_tst3(s_name):
> # Runs the initial subprocess.Popen command - creates proc.
> # Hangs when you try to use proc.communicate
> proc = subprocess.Popen(['C:\\Progra~1\\putty\\plink','Sula'],
> shell=True,
> stdin=subprocess.PIPE,
> stdout=subprocess.PIPE,
> )
> #Either of the next two commands cause window to hang
> #proc.stdin.write("dir")
> #(stdout_value, stderr_value) = proc.communicate(input="dir")[0]
> return proc
>  
> Thanks,
>  
> Mike
> ___
> 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] Is it pythonesque

2010-01-24 Thread शंतनू
If the input has more than one ',', e.g. '1,2,3'. You may like to use 'split' 
function, and avoid find.

On 24-Jan-2010, at 9:43 PM, Robert Berman wrote:

> Good morning,
>  
> Given the following code snippets:
>  
> def getuserinput():
> while True:
> s1 = raw_input('Enter fraction as N,D or 0,0 to exit>>')
> delim = s1.find(',')
> if delim < 0:
> print 'invalid user input'
> else:
> n = int(s1[0:delim])
> d = int(s1[delim+1::])
> return n,d
>  
> def main():
> while True:
> n,d = getuserinput()
> if n == 0 or d == 0: return 0
>  
> n and d are always returned provided the input is given as n,d. n/d will 
> print an error message and the loop will reiterate until the user format is 
> correct. Please note there is no true ending return for getuserinput() as it 
> is hung off an if statement and if by some  chance it breaks, it should 
> return None which will abort the program. While I suspect this style is 
> devious, is it dangerous or ‘wrong’ to use. Comments and other renditions are 
> most welcome.
>  
> Thank you,
>  
> Robert Berman
>  
> ___
> 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] Testing for empty list

2009-10-18 Thread शंतनू


On 19-Oct-09, at 7:11 AM, vince spicer wrote:




On Sun, Oct 18, 2009 at 7:29 PM, Wayne  wrote:
Hi, I think I recall seeing this here, but I wanted to make sure I'm  
correct.


Is the best way to test for an empty list just test for the truth  
value? I.e.


mylist = [1,2,3]

while mylist:
   print mylist.pop()

Thanks,
Wayne


I believe it is better to check the list length, I think some  
changes are coming on evaling list



mylist = [1,2,3]

while len(mylist) > 0:
   print mylist.pop()


or

while mylist != []:
print mylist.pop()


or for reverse iteration without popping the values in the list.


for x in reversed(mylist):
print x


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


Re: [Tutor] how to get blank value

2009-07-28 Thread शंतनू

You may like to have a look @ pyparsing module.
URL: http://pyparsing.wikispaces.com/

HTH.
regards,
shantanoo

On 29-Jul-09, at 1:03 AM, amr...@iisermohali.ac.in wrote:


with these data it is giving output but when I tried

if __name__ == '__main__':
data = open('chem.txt').readlines()
for line in data:
line2 = line.split('=')
if not line2[5]: # C value missing
  if len(line2[2]) <= 5 and len(line2[3]) <= 5:  # N and CA values
missing
print "all missing", line
  else:
print "C missing", line

by putting data in .txt file then it is not giving output. Actually  
I have
few large data file what I want that I will put those lines in one  
file
for which only C value is missing and in another one I will put  
those line

for which N, CA and C all values are missing.

Thanks for help.
Amrita


amr...@iisermohali.ac.in wrote:

It is not giving any value, without any error

ph08...@sys53:~> python trial.py
ph08...@sys53:~>
it is coming out from shell.


Try this. I embedded the test data to simplify testing:

data = """48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50
104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C =
85 ALA H = 8.60 N =  CA =  HA = 4.65 C =""".split('\n')
for line in data:
  line2 = line.split('=')
  if not line2[5]: # C value missing
if len(line2[2]) <= 5 and len(line2[3]) <= 5: # N and CA values
missing
  print "all missing", line
else:
  print "C missing", line

--
Bob Gailer
Chapel Hill NC
919-636-4239




Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA

___
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] How o convert spaces into tabs??

2009-06-02 Thread शंतनू (Shantanoo)



jyotsna guleria wrote:

Hello every one,

I am trying to parse a file:

I want to convert all the spaces in between the characters to single tab.

e.g: my file has contents like:

1G579011  10  2   0  0
0   0   0   00
5Ht-2  60459  11  0   0  0
0   0   0   00



I want them to be separated by a single tab not with spaces..

It should look like:

1G5790111020000000
5Ht-2604591100000000

each separated by Tab...

It is a text file containing such a data ..


You can try re module.
More info at:
http://docs.python.org/library/re.html
http://www.regular-expressions.info/python.html

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


Re: [Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread शंतनू (Shantanoo)
On Wed, Nov 19, 2008 at 4:48 PM, amit sethi <[EMAIL PROTECTED]>
wrote:
> Hi , i am trying to learn python and this is my first time with any
> databases . I am using sqlite3 to create a database of my music files and
> its
> metadata tags so here is what i wanted to do . Two list one of the
> attributes and one of their values ,how do i put it in the database.Here
is
> a simple code i think should work but isn't?
 import sqlite3
 conn=sqlite3.connect('/tmp/example2')
 c = conn.cursor()
>
 list1=['hello','hi']
 list2=['a','b']
c.execute('''create table ABC(hello text,hi text)''')
 list1_value= ",".join(list1)
 list2_value= ",".join(list2)
 c.execute('''insert into ABC (%s) values
 (%s)''')%(list1_value,list2_value)

You may try:
c.execute("insert into ABC(%s) values('%s')" % (list1_value, list2_value))

regards,
shantanoo
-- 

Fred Allen  - "An associate producer is the only guy in Hollywood who will
associate with a producer."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 51, Issue 23

2008-05-11 Thread शंतनू महाजन (Shant anoo Mahajan)

Hi Jatinder,
Please turn off caps lock.
On 11-May-08, at 4:54 PM, root wrote:


Hi ,
I AM TRYING FOR A PYTHON PROGRAM THAT WILL CHECK WHETHER {- PHP AND
MYSQL -} ARE INSTALLED ON lINUX OR NOT AND IF THEY ARE NOT INSTALLED
THEN THE SCRIPT WILL TO INSTALL IT.

for this I am doing

import sys
import os
import commands
import sys


commands_hash = {}
commands_hash['php'] = {}
commands_hash['php']['title'] = "***PHP**"
commands_hash['php']['value'] = "rpm -qa *php*"
commands_hash['mysql'] = {}
commands_hash['mysql']['title'] = "***MYSQL**"
commands_hash['mysql']['value'] = "rpm -qa *mysql*"


this is checking whether PHP and MYSQL are installed but PLEASE TELL  
ME

HOW TO ENHANCE IT SO AS THE INSTALL IT IF NOT INSTALLED ALREADY.


Please check '-i' and '-U' options of rpm command.

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