[issue23794] http package should support HTTP/2
Changes by Boris Dušek <m...@dusek.me>: -- nosy: +dusek ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue23794> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation)
Hi, I am looking for the best way to convert a string of length 1 (= 1 character as string) to integer that has the same value as numeric representation of that character. Background: I am writing functions abstracting endianness, e.g. converting a string of length 4 to the appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big endian memory, 2**0 for little endian memory). For this, I need to know the numeric value of each byte and sum them according to endianness. I thought that something like int('\x01') might work, provided the argument is string of length 1, but that throws an error: int('\x12') Traceback (most recent call last): File stdin, line 1, in ? ValueError: invalid literal for int(): The code I want to write looks like this: mem = '\x11\x22\x33\x44' factor = 1 sum = 0 for byte in mem: sum += int(byte) * factor factor *= 2**8 Could you please tell me how to achieve what I want in Python? (it would be straightforward in C) Thanks for any suggestions, Boris Dušek -- http://mail.python.org/mailman/listinfo/python-list
Re: int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation)
On Sep 15, 1:59 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: So you are looking for the `struct` module in the standard library instead of doing this yourself. :-) If you insist on doing it yourself take a look at the built-in `ord()` function. Thanks Marc, both things are exactly what I was looking for (I am a bit ashamed that I did not find the built-in ord :-) Boris -- http://mail.python.org/mailman/listinfo/python-list
Base class for file-like objects? (a.k.a Stream in Java)
Hello, (sorry to begin with Java in a Python list ;-) in Java, when I want to pass input to a function, I pass InputStream, which is a base class of any input stream. In Python, I found that file objects exist. While specifying argument types in Python is not possible as in Java, it is possible to check whether an object is an instance of some class and that's what I need - I need to check if an argument is a file-like object, and if yes, behave accordingly, if not, treat the argument as string with URL. But I am afraid there is no such a base class - I tried the following: import urllib f = open(test.txt, r) g = urllib.urlopen(http://www.google.com/;) isinstance(f, file) True isinstance(f, file) False ... Is there some base class to file-like (or stream-like) objects in Python? And if not, is it at least planned for Python 3.0? Thanks for any suggestions, Boris Dušek P.S.: The code should finally look in esence something like this: if isinstance(f, file): pass elif isinstance(f, string): f = urllib.urlopen(f) else: raise ... process_stream(f) -- http://mail.python.org/mailman/listinfo/python-list
Significance of start parameter to string method endswith
Hello, what is the use-case of parameter start in string's endswith method? Consider the following minimal example: a = testing suffix=ing a.endswith(suffix, 2) Significance of end is obvious. But not so for start. Let's assume the end parameter is not used - then the function should simple check that the last len(suffix) characters of a are equal to ing, no matter where we start (the function does not *scan* the string from the start, does it?) Only case where it would make difference is if we had start + len(suffix) len(a) (excuse possible of-by-one error :-) Then the function would never return True. But is there a real use case when we would test for endswith like this? (knowing that it must return false?) Thanks for any ideas/experience. Boris -- http://mail.python.org/mailman/listinfo/python-list
Re: Significance of start parameter to string method endswith
On Apr 19, 10:36 pm, subscriber123 [EMAIL PROTECTED] wrote: On Apr 19, 3:58 pm, Boris Dušek [EMAIL PROTECTED] wrote: Hello, what is the use-case of parameter start in string's endswith method? def foo(function,instance,param): if function(instance,param,2,4): return True else: return False The function must work whether you pass it foo(str.endswith,blaahh,ahh), or foo(str.startswith,blaahh,aah). This is a really bad example, but it gets the point across that similar functions must have similar parameters in order to be Pythonic. Thanks for explanation, this point makes sense. And I agree that I can hardly imagine any use of both parameters :-). -- http://mail.python.org/mailman/listinfo/python-list