[Tutor] Pulling items from a dict in a print command

2014-06-18 Thread Ben Sherman
Whats a more pythony way to do this?  I have a dict with a few dozen
elements, and I want to pull a few out.  I've already shortened it with
itemgetter, but it still seems redundant.  I feel like I can do something
like I've seen with *kwargs, but I'm not sure.

I'm using old style sprintf formatting, so feel free to show me a better
way to do this with the new way.

Thanks for the help!

Code:

print(("overall_status=%s|" +
"mon_count=%s," +
"healthy_mons=%s," +
"pg_count=%s," +
"pg_clean_count=%s," +
"osd_count=%s," +
"osd_up=%s," +
"osd_in=%s," +
"bytes_avail=%s," +
"bytes_used=%s," +
"bytes_total=%s") %
itemgetter("overall_status",
"mon_count",
"healthy_mons",
"pg_count",
"pg_clean_count",
"osd_count",
"osd_up",
"osd_in",
"bytes_avail",
"bytes_used",
"bytes_total")(parsed_json))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] String module; Count

2007-10-17 Thread Ben Sherman
You can only count one at a time.

count = conversion(n).count("0") + conversion(n).count("1")

count is a string method, so it operates directly on the string - you
don't have to call it like you did.

import string
string.count(mystr, "cheese")

is the same as

mystr.count("cheese")

At least it is in newer versions of python.

Let me know if that helped.

Cheers,
Ben

On 10/17/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I am having trouble getting the string.count function to work. I want it to
> count the amount of digits (0 or 1) in the string, but I keep getting an
> error stating the string.count was expecting a character buffer object.
> CODE:
> count = string.count(conversion(n),["0","1"])
>
> ERROR:
> Traceback (most recent call last):
>   File "/Users//Desktop/Project 1.py", line 59, in -toplevel-
> signed_mag()
>   File "/Users//Desktop/Project 1.py", line 29, in signed_mag
> count = string.count(conversion(n),["0","1"])
>   File
> "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/string.py"
> , line 348, in count
> return s.count(*args)
> TypeError: expected a character buffer object
>
> I'm trying to make a decimal to binary converter that has the option to
> select the amount of bits for Signed Binary. I've thought of a way (not
> tested yet) on how to implement the bits, but I first need to count the
> amount of digits in the original conversion. Thanks in advance.
> ___
> 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] encryption for files/passwords

2007-05-18 Thread Ben Sherman

On 5/18/07, Rohan Deshpande <[EMAIL PROTECTED]> wrote:


Hey all,

I am writing a small python script to maintain some passwords and
identity info.  All the data is in an external file.  what is the best
way to encrypt/decrypt this file's data using a key? I am new to
encryption methods let alone how to do it in python.  I had a look at
python-crypto, ezPyCrypto and yawPyCrypto but they seemed overkill?



If you only need to encrypt passwords, look at the crypt module - it does
one way password hashing.

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


Re: [Tutor] Learning to Debug?

2007-05-16 Thread Ben Sherman

On 5/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:


I'm moving forward with my learning of Python, but I've decided it's
finally time to address a problem that has haunted me in every language I've
every tried to learn: debugging.   I'm just not very good at it. Does anyone
have recommendations for Python-centric books/online tutorials that teach
you techniques for good debugging?





Hi David, welcome to Python!

You should look at the pdb module.  A good tutorial is at
http://www.ferg.org/papers/debugging_in_python.html

Good luck!
Ben
(accidentally replied directly to David the first time)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aaagh! Stack arrays!?! calc average

2007-05-01 Thread Ben Sherman

On 5/1/07, John Washakie <[EMAIL PROTECTED]> wrote:


It aint pretty! And if I had just walked away, it probably would've
taken half the time in the morning, but here's what I've come up with
(any suggestions for improvements, or course are welcome):

for d in data:
w = len(d)
if d[0] <= tinit+60:
d = column_stack(d)
cnt,sum = cnt+1,sum+d

else:
avg = sum/(ones(w)*cnt)
tinit,cnt,sum = d[0],0,zeros(n)
if init==0:
newData,init = avg,1
else:
newData = append(newData,avg,axis=0)

return newData





Sorry my last reply was so terse - I needed to catch a train. :)

So you need to check out a couple of functions - namely sum.


numbers=[1,2,3,4,5]
numbers.append(6)
numbers

[1, 2, 3, 4, 5, 6]

sum(numbers)

21

len(numbers)

6

sum(numbers)/len(numbers)

3

WAIT WAIT hold the phone!?  21/5 is NOT 3!  It's 3.5!  The short story here
is that you have to make on of the numbers a floating point to get the
result to return a float, so do this:


sum(numbers)*1.0/len(numbers)

3.5

So there is our average.  If floor division doesn't make sense to you, you
aren't alone.  This changes in Python 3000.  You can read about floor
division here:
http://www.python.org/doc/2.2.3/whatsnew/node7.html

I don't quite know what your stack is for - it can probably be accomplished
using some slices or other fun stuff.

Good luck, and welcome to python!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aaagh! Stack arrays!?! calc average

2007-05-01 Thread Ben Sherman

On 5/1/07, John Washakie <[EMAIL PROTECTED]> wrote:


Ug,

It still doesn't make sense due to the sum/cnt where cnt is just an
int, and sum is a 1-dimensional array!

I'm missing something here about working with numpy arrays...



You need to add all of  the numbers in your list - you can't just divide a
list by an int.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aaagh! Stack arrays!?! calc average

2007-05-01 Thread Ben Sherman

On 5/1/07, John Washakie <[EMAIL PROTECTED]> wrote:


Oops, I meant it crashes at line 7..

>
> 1) tinit = data[0][0]
> 2)for d in data:
> 3)if d[0] <= tinit+60:
> 4)sum = sum+d
> 5)else:
> 6)avg = sum/len(sum)
> 7)newData = append([newData],[avg],axis=0)
> 8)tinit = d[0]
>



You didn't include your append() function here, so it's hard to tell whats
going on.  Perhaps you mean to use newData.append(avg) assuming that newData
is a list?

Also, your average looks funky.  If sum is something you can get a len() on,
you can't divide it.  It should be bombing here.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fixing garbled email addresses

2007-05-01 Thread Ben Sherman

On 5/1/07, Dotan Cohen <[EMAIL PROTECTED]> wrote:


[snip]
>
> List comprehensions are the best thing ever!
>
> Happy to help,
> Ben
>


With Gmail one must be careful and check that the To and Subject
fields contain what you'd expect.

Does 'list comprehension' mean a detailed explanation of the code? If
so, then I'll be reading a lot of them in the near future. I really do
appreciate the dedication and attention to detail. Thanks.




List comprehension are a python/programming term.  They allow one to make a
list without generating a blank one and appending to it. A very basic
example:

newlist = [item for item in spam(eggs)]

is the same as

newlist = []
for item in spam(eggs):
   newlist.append(item)

If you walk through the code from earlier, you can see where a list
comprehension saved some lines and made the flow easier to read.

Official documentation is here: http://docs.python.org/tut/node7.html(section
5.1.4)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fixing garbled email addresses

2007-05-01 Thread Ben Sherman

On 5/1/07, Dotan Cohen <[EMAIL PROTECTED]> wrote:

I have had the misfortune of having a university Windows machine
garble all the email addresses in my addressbook (a txt file so that I
can use it both on my home Fedora machine and on the university
Windows machines). I figure this is as good a time as any to start
learning python and fix the file. How can I iteriate through a text
file that looks like this:

 "=?UTF-8?B?157XqNeZ15Qg15nXoNeY16bXnw==?=" <[EMAIL PROTECTED]>,
 "=?UTF-8?B?157XqNenINen15nXmNek15XXkQ==?=" <[EMAIL PROTECTED]>,
 "=?UTF-8?B?157XqdeUINem15LXkNeZ?=" <[EMAIL PROTECTED]>,

and have it return:
[EMAIL PROTECTED],
[EMAIL PROTECTED],
[EMAIL PROTECTED],

Thanks in advance.

Dotan Cohen

http://lyricslist.com/
http://what-is-what.com/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



Hi Dotan!  Welcome to python!

Here is some code that will do what you need.  It uses the re module,
which are regular expressions.

# You need to import the module:
import re

# Then you need to read in the file that contains your list.
email_list = open("brokenemails.txt","r")

# We need to generate your regular expression.  The grabs anything in
# the file that is between < and >, but it includes the <>
re_mail=re.compile(r"\<(.*)\>")

# Then filter each line of the file through the regex, discarding the
# <> from above, and puts each address into a list.
addresses = [re_mail.search(line).group(1) for line in
email_list.readlines()]

# Now we print them out, comma and newline separated
print ",\n".join(addresses)

Let me know if you need more detail!

Your pal,
Ben
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] scope/namespaces

2007-04-24 Thread Ben Sherman
Am I wrong in my memory?  When I was a wee lad prior to 99 for sure),
I thought I would initialize my loops with:

for (int x=0; x <10; x++) {
}

I am rapidly veering off topic.


On 4/24/07, Alan Gauld <[EMAIL PROTECTED]> wrote:
> Correcting my own post!
>
> "Alan Gauld" <[EMAIL PROTECTED]> wrote
>
> > That's a very recent change to C/C++ (1999 apparently),
>
> Actually only a recent change in C. Its always been true of C++.
> But in C up until recently(*) you couldn't define a loop
> variable in the loop it had to be outside:
>
> int x;
> for (x=0;)
>
> (*)I'm not sure whether the C++ style loop definition was
> introduced in the original ANSI standard or the later
> revision (none of my books malke it clear), but I think it was
> the revision.
>
> But C++ always had loop variables as part of block scope.
>
> Sory for any confusion,
>
> Alan G.
>
>
>
> ___
> 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] raw_input into range() function

2007-04-18 Thread Ben Sherman
On 4/18/07, Guba <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I am trying to do the exercises in Michael Dawson's "Absolute Beginner"
> book. In chapter four ("for Loops, Strings, and Tuples") one of the
> challenges is: "Write a program that counts for the user. Let the user
> enter the starting number, the ending number, and the amount by which to
> count."
>
> The code I have come up with so far is further below; basically my
> problem is that I don't know how to feed the range() function with the
> user-input numbers it expects.
>
> Your help is highly appreciated!
>
> Guba
>
>
> # Counting Program
> # 2007-04-18
>
> # Welcoming the player
> print "Hello, let me do some counting for you!"
>
> # Telling the player what to do & assigning that info to variables.
> start_num = int(raw_input("Please give me a starting number. "))
> end_num = int(raw_input("Please give me an ending number. "))
> interval = int(raw_input("By which amount am I to count? "))
>
> start_num == 0
> end_num == 1
> interval == 2
>
> print "Counting:"
> for i in range(0, 1, 2):
>  print i
>
>
> raw_input("\n\nHit Enter to exit.")


Your attempt to read input is never used, and your variable
assignments are not correct.

You are using the test operator '==' instead of the assignment
operator '='.  The lines:
"""
start_num == 0
end_num == 1
interval == 2
"""
do something you aren't trying to do here.  Those lines are testing to
see if start_num equals zero, and then ignoring what the test result
is.  They don't actually *do* anything.

Your code should look like this:

print "Hello, let me do some counting for you!"

start_num = int(raw_input("Please give me a starting number. "))
end_num = int(raw_input("Please give me an ending number. "))
interval = int(raw_input("By which amount am I to count? "))

print "Counting:"
for i in range(start_num, end_num, interval):
print i
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] please help me!

2007-04-12 Thread Ben Sherman
You

On 4/12/07, suryo agung <[EMAIL PROTECTED]> wrote:
> pleate tell me how to make
>
> input number=4
> result
>
> 1
> 22
> 333
> 
>
> in python
> please give me your answer.

input_number = 4

for i in range(1,input_number + 1):
  print str(i) * i

If this is homework, please tell your teacher I helped - I need the
extra credit.
___
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor


[Tutor] List slicing and joining

2007-04-11 Thread Ben Sherman
I've got a list that contain a bunch of information, including the
FQDN of a host.

host_data=['foo.example.com', 'other unimportant data']

I need to seperate the hostname from the domain name.

This is how I'm doing it, and it work, but it seems *really* hacky.
Is there a better (or more pythony) way?

hostname=host_data[0].split(".")[0]
domain=".".join(host_data[0].split(".")[1:])

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