scru 909

I'm not sure I fully understand your questions, but the reason your addFirst does not work seems to be line 19:

Should be [icode]list.head = first[/icode] instead of [icode]list.head == first[/icode]

_neo_ commented: great! +1
scru 909

If I have any dependencies that require 2x, I use 2x. Otherwise I use 3.

scru 909

[QUOTE=SoulMazer;1076370]Okay, well I've been looking for at least an hour now for some straight-forward python-xlib documentation, and I have been unable to find any. Luckily...I have quite the easy problem.

If somebody would be able to help me create a simple script that would say "Hello" every time the left mouse button is clicked, I have the rest of what I would like to do figured out

This is what I have so far, to no avail:

import Xlib
import Xlib.display

def main():
display = Xlib.display.Display()
root = display.screen().root
while True:
event = root.display.next_event()
if event:
print "Hello"

if name == "main":
Thank you so much in advance.[/QUOTE]

This should help you out. It's written in C++, but the Xlib wrapper for python is very light so it shouldn't be hard to translate.

Have fun.

scru 909

[QUOTE=mn_kthompson;1047514]I guess it depends on what you mean by simplify. If you mean fewer lines of code, then yeah there are ways to simplify it.

If you mean make the code more readable so that the next person who has to maintain your code will be able to, then probably not. The code you've written is easy to follow, well commented, and doesn't make use of any obscure libraries or tricks. Your variable names make sense and the whole thing is easy on my eyes. Personally, I would be proud of this code. You might be able to shave a few microseconds here or there by doing something, but if it comes at the expense of readability then it isn't worth it.[/QUOTE]

Here here!

That said, I would suggest you use [icode]if x in dict.keys()[/icode] rather than [icode]if dict.has_key()[/icode] since the latter is deprecated in later versions of python (it's never too early to start preparing yourself for the switch, even if you don't intend to anytime soon). Besides, it's slightly closer to what you would actually say in English, which makes it slightly easier to follow. Slightly.

scru 909

[QUOTE=vnproduktionz;1028310]im taking a class in python but i just cant keep up with the curriculum. Some things confuse me so i was wondering if anyone have any websites or anything that can help me get through python or at least help me understand it easier. the book im using right now is python programming second edition for the absolute beginner.[/QUOTE]

Ooh. didn't notice you were already using the book that I recommended (Incidentally the one I learned Python with). Have you tried the official Python tutorial?


scru 909

[QUOTE=vegaseat;1026220]What you have in your class right now are functions, not methods. You can turn your functions into methods with 'self'. This way you can also avoid globals outside the class, like your method list.[/QUOTE]

Whoa hey I did not even notice that! Yeah dude, to make methods in python Classes, set self as the first argument of every routine. To call a method from any method within the same class, do [icode]self.methodname()[/icode] (note that you don't put self back in, when calling methods, python automatically sends in the object instance, ergo self, as the first argument.)

scru 909

Well, it's [I]bad[/I] OOP.

Not that I actually think that everything has to be OOP mind you (some small programs are just better off without it), but take for example your methods list. Why is your class directly manipulating an external object which it does not own (or was not given)?

What does displaying a menu have to do with a calculator? I don't necessarily agree that you need a class for something so small though. Perhaps a function that returns the index of the function selected, or better yet the name (so you can just do [icode]getattr(c, name)()[/icode])

scru 909

Sure, C++ is difficult for a beginner, but I recommend you go with it first if it is the only one being offered at your school. You will have the benefit of face to face support from your lecturers and peers (nothing beats that!). You may as well get it out of the way early with as much help as possible. Perhaps you can even continue with python in your spare time.

scru 909

locals_dict = locals()
for i in range(1, 10):
var_name = "POS" + str(i)
if i == POS:
locals_dict[var_name] = "->"
locals_dict[var_name] = " "

discalimer: That code uses hacks and the purists may have your head for it.

scru 909

If you a using a GUI/event-driven framework, then yes, it's possible. If you are using console python, then no.

scru 909

I'd like to see you try to implement one of the existing encryption algos, much less to coming up with your own.

Thought so.

scru 909

[QUOTE=foosion;984309]One byte is a number, two bytes is a string of bytes. One str element is a string, two string elements is a string. If you accept that, it all makes sense. However, it seems to have changed from 2.6 to 3.1.

The reason I got into this was trying to port a 2.6 app to 3.1. The code reads some bytes from a file, then examines the bytes.

[code]f = open(filename, 'rb')
data =
if data[0:2] == '\xFF\xD8':
if data[2] == '\xFF' and data[6:10] == 'Exif':

Note that both single bytes and the sequence of bytes are treated the same, and that they don't require any 'casting' for the comparisons.

This works in 2.6, but fails in 3.1. 3.1 requires
[code]if data[0:2] == b'\xFF\xD8':
if data[2] == ord('\xFF') and data[6:10] == b'Exif':

Note that the single byte is treated differently than the sequence, and that we need to add 'b' and 'ord'.

As an aside, haven't we eliminated longs in 3.1, so that all numbers are of type int?[/QUOTE]

bytes, as they exist in Python 3.1 are conceptually new to Python (starting with version 3). This isn't to say that 8-bit strings didn't exist before in the form of regular text strings. Note the distinction, each element in a 8-bit python 2.x string (str) is treated as a character, not a byte, yielding the same slicing and indexing behavior as str in Python 3.1. Note that the bytes type in Python 2.6 is a synonym for ...

vegaseat commented: Thanks for the help +14
scru 909

[QUOTE=foosion;983956]Is this in the documentation somewhere?

[code]>>> a = b'\x01\x02'

<class 'int'>
<class 'bytes'>
a = '\x01\x02'
<class 'str'>
<class 'str'>[/code]

Note that in the byte string version, a[0] and a[0:1] return different types, while in the regular string version, both return the same type. Why does it make sense to treat the two cases differently?

I would have expected python to be more consistent.

Here's a similar example:
[code]>>> s = "hello"

<class 'str'>
b = s.encode()
<class 'bytes'>

I happen to think this behavior is consistent (with indexing and slicing rules).

Here's why it makes sense:

bytes and str are [B]not[/B] the same. They aren't even conceptually the same.

A bytes (byte string) is a sequence of bytes (I assume you know what a byte is, and that it isn't a "character"). It is data, [B]not text[/B]. A string on the other hand is a sequence of [B]characters[/B] and is text.

As I mentioned earlier, indexing a byte string gives you a byte (it's a [B]sequence of bytes[/B], so this makes perfect sense). Since a byte is a numeric type (and [B]not a character[/B]) what you get is a number (long). Slicing a sequence gives you the portion of the sequence from a to (but not including) b as a new sequence. Note that slicing a sequence always gives a sequence. Why? ...

scru 909

[QUOTE=Gribouillis;983820]More exotic
from itertools import takewhile, count
c = sum(1 for b in takewhile(lambda x: x != a, (int(random.triangular(1, 100)) for x in count())))

You actually think that is more exotic? Wow.

scru 909

No. b'\x01\x02' Is a byte string in python. The expression [icode]bs[x][/icode] means that you want the [B]byte[/B] at position x in the bytestring x, while [icode]bs[a:b][/icode] means that you want the part of the byte string from a to (but not including) b. Not the difference: indexing gives a byte (as long), and slicing gives a byte string. The reason [icode]bs[0] != bs[0:1][/icode] is because they are different types.

scru 909


No, python doesn't have clear screen.

scru 909

[QUOTE=mahela007;978866]Thanks for your help.. that works for me....
However, if anyone knows how to actually generate a variable which has the name of an input provided, please post a code snippet in this thread.[B] Hopefully, someone will be able to learn something.[/B][/QUOTE] (emphasis mine)

No, they won't. You see, there's no good reason anybody would want to do that. I guarantee you that anybody wanting to do this has a totally different underlying problem to which they have ignorantly attached their own silly possible solution.

scru 909 supposedly loves spam anyway, so long as you include some eggs.

scru 909

Look, not to be harsh or anything, but given you previous posts I think you would be better off reading a book or a wiki or even just a tutorial on Python OOP and maybe Python in general. Really.

vegaseat commented: glad you said it! +14
scru 909

[QUOTE=scru;978840]Whenever I try to view anyone's profile I get: (see attachment)[/QUOTE]

Huh. Now I can see profiles again.

scru 909

Whenever I try to view anyone's profile I get: (see attachment)

scru 909

Okay, I will, but first give me [B]one[/B] reasonable use for this technique.

scru 909

[QUOTE=leegeorg07;960030]basically what bumsfield said is right but i like to think of them as the dll's of the python world[/QUOTE]

Well they actually really [I]are [/I]dlls. It's just that the routines inside are built on the python C API.

To use them, just import them like any other module.

scru 909

What vegaseat said.

And if you're trying to optimise your program by using the most "efficient" of these, you have issues.

scru 909

[QUOTE=Lingson;955374]ugh.. the solution was so easy? lol

one more question then:
is it safe to say that everytime i want to convert bytearrays to string i just need to use the .decode('utf8')?...some other stuff...[/QUOTE]

No, please don't do that.

That is okay if the page you are reading is encoded in ascii or utf-8. But if it is encoded in latin-1 (a fairly common encoding in its own right), you can run into trouble with non-ascii characters (like é for example)). And this is just for sites in languages based on latin script.

What you have to do is get the encoding from the server. The web server sends out the encoding that it uses* in the http header. The general idea is that the pages it serves would use that encoding.

  • If only it were that simple. Sometimes the pages on the web server itself are encoded using a different encoding than what the web server is set to report (blame it on lazy, inconsistent webmasters?) Ideally, these pages have their encoding specified in their HTML headers.

Oh right, you were looking for a solution? I generally start decoding with the encoding that the http header specifies, but if the page's html header specifies a different encoding, I start over again with that new encoding. And if all else fails, either fall back to utf8 or refuse to decode all together. I know it sounds overly complicated (it probably is) but it's worth not having your application crash ...

Nick Evan commented: excellent suggestion +25
scru 909
  1. You control the way your Car class prints by adding an str method to it. Here is an example:[code=python]
    def str(self):
    s = self.brand, +": $", self.price #eg: Dodge Viper: $80000
    return s[/code]

  2. Because you're not checking the list properly. You are asking if the string the user has entered is in the list (it's not), not if the the list has a car with a name that matches the users entry. Here is a function that can help:[code=python]
    def car_in_list(l, n):
    for car in l:
    if n ==
    return True #car with matching name found
    return False #no car found with name
    Instead of [icode]if x in carlist[/icode], you would use [icode] if car_in_list(carlist, x)[/icode]

scru 909

I'm not even going to lecture you about why that is just horrible UX. If you want to put your users through that kind of torture, it's your call. The principle of what you're trying to achieve is the same no matter what toolkit you use. I don't use Tkinter though, so here's pseudocode (just put it in a routine that gets called every time the text in the entry changes):

for each character in the entry field:
If the character entered is not a number:
delete the character
set deleted flag

if deleted flag is set:
deploy annoying message box

Of course, it's better to just [B]use a control that was designed to input numbers.[/B] (A spinner or slider, depending on what the number being entered represents.)

scru 909

Well, I'm not Dan, but here's a little hack adapted from [url][/url] for Python. It's only two lines of code!
To open:
import ctypes
ctypes.windll.winmm.mciSendStringW("set cdaudio door open", None, 0, None)

To close:
import ctypes
ctypes.windll.winmm.mciSendStringW("set cdaudio door closed", None, 0, None)

Note, if you're using python 2.x, either change mciSendStringW to mciSendStringA, or change the first argument to a unicode string (recommended) by prefixing it with a lowercase u.

vegaseat commented: nice Windows hack +12
scru 909

That's one reason [B]not[/B] to sign.

mvmalderen commented: Agreed :) +17
William Hemsworth commented: haha :D +13
scru 909

This method is probably a bit less efficient than other suggestions you'll get, but it's just one line:

new_list = [item[:1] for item in old_list]

Question. Why don't you use tuples instead of lists for the inner structure? They use less memory than lists.