Re: can't get utf8 / unicode strings from embedded python
David M. Cotter wrote: > Steven wrote: >> I see you are using Python 2 > correct > >>It's hard to say what *exactly* is happening here, because you don't explain >>how the python print statement somehow gets into your C++ Log code. Do I >>guess right that it catches stdout? > yes, i'm redirecting stdout to my own custom print class, and then from that > function i call into my embedded C++ print function > I don't know much about embedding Python, but each file object has an encoding property. Why not examine sys.stdout.encoding ? And change it to "UTF-8" ? print "encoding is", sys.stdout.encoding sys.stdout.encoding = "UTF-8" -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
snarf wrote: > Greetings, > > As I tread through my journey of OO I am trying to determine if there is a > good approach for exception handling within classes. > > From my readings and gatherings - it seems I have found a common theme, but I > am trying to solicit from the experts. > > Here is what I have found (I may be restating the obvious so please forgive > me in advance): > > * Seems like exception handing within Classes is largely avoided and is > typically only used when calling external libraries. > * Try/Except type statements seem to be used more within modules, main > functions, wrapper scripts. Exceptions are used when useful. I don't see any bias towards any one location. > * Classes should be coded in a way that exceptions You seem to be missing the last part of this sentence. > * Better to never write your own exceptions (unless you absolutely have to). If you mean to avoid writing exception classes, then I say nonsense. Just derive them from the closest meaningful exception class, so that a user can combine handlers when reasonable. > * Using Exception is typically a bad. More specific the better. If you mean in an except statement, then I'd agree. > * Exceptions should never fail silently. (Should exceptions always be logged?) Exceptions should be caught if you can handle them, or if you need to convert them to a different exception that someone further up the stack can handle. Sometimes handling means do nothing. > > Best site I have found for exceptions (hopefully this helps someone): > * http://c2.com/cgi/wiki?ExceptionPatterns But that's for Java. java is not C++, and neither is it Python. For one thing, Python exception overhead is deliberately much less, and they are used more freely. Notice that exceptions are used to terminate for loops, and that's a *normal* exit to the loop. They also appear in other places under the covers. Don't be afraid of them. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast conversion of numbers to numerator/denominator pairs
On Fri, Aug 23, 2013 at 9:30 PM, Steven D'Aprano wrote: > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > > which is faster, but not fast enough. Any suggestions? I time this function at about 33% faster than your version for a six-digit decimal, and almost 50% faster for a 12-digit decimal. My guess would be because it's not calling str() on every individual digit. def convert(d): exp = d.as_tuple().exponent num = int(d.scaleb(-exp)) return num, 10**-exp -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast conversion of numbers to numerator/denominator pairs
On Sat, Aug 24, 2013 at 1:37 AM, Ian Kelly wrote: > > I time this function at about 33% faster than your version for a > six-digit decimal, and almost 50% faster for a 12-digit decimal. My > guess would be because it's not calling str() on every individual > digit. > > def convert(d): > exp = d.as_tuple().exponent > num = int(d.scaleb(-exp)) > return num, 10**-exp Although, you would need to be careful with handling the decimal context for the scaleb operation to make sure the result is exact. -- http://mail.python.org/mailman/listinfo/python-list
Re: Running a command line program and reading the result as it runs
Peter Otten wrote: Ian Simcock wrote: Greetings all. I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work. import subprocess p = subprocess.Popen("D:\Python\Python27\Scripts\pip.exe list -o", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True, shell=False) for line in p.stdout: print line When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking? The following works on my linux system: import subprocess p = subprocess.Popen( ["ping", "google.com"], stdout=subprocess.PIPE) instream = iter(p.stdout.readline, "") for line in instream: print line.rstrip() I don't have Windows available to test, but if it works there, too, the problem is the internal buffer used by Python's implementation of file iteration rather than the OS. Hmm... and so it comes full circle. I thought that the inclusion of the iter call looked familiar so I checked my original code and found that it was there. I removed it when shrinking the code down to a minimal example for posting. Then, after removing it, which triggered the blocking, I changed the command to ping so that it's easier for anyone to test. I've tried a copy of my original code using the ping command and it works fine. So it looks like the python package manager pip must see that it's not going to a console and buffer the output, and my original code was not the problem. So I can't do what I want, but it's interesting to know that for some reason the iter is required for the occasions when it can work. Thanks to everyone who helped with this. Ian Simcock -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [...] > * Seems like exception handing within Classes is largely avoided and is > typically only used when calling external libraries. There is certainly no rule "avoid exceptions inside classes". Methods often raise exceptions to signal an error, e.g.: "My string".index("spam") Less common, methods can raise exceptions as part of their flow control. The most obvious example is StopIteration, used by iterators and often by __iter__ or next methods. > * Try/Except type > statements seem to be used more within modules, main functions, wrapper > scripts. It depends on whose code you are reading. I don't write a lot of classes, but when I do, I often use try...except inside them. If try...except gets used more frequently in module's top level, it is because the sort of things that you do at the top level often needs exception handling. For example, you might have a fallback module: try: import this_module except ImportError: import that_module as this_module You will very rarely see that inside a class, since you very rarely import modules inside a class. > * Classes should be coded in a way that exceptions I think you forgot to finish the sentence. > * Better to > never write your own exceptions (unless you absolutely have to). That depends. On the one hand, nobody wants a million different exception types. On the other hand, nobody wants just *one* exception type, and no way to distinguish between different kinds of errors. Somewhere between one and one million is an appropriate number of exception types. The right answer is to be conservative about creating new exceptions, but don't be scared to create one when you need one. But when you do, it is often better to subclass from an appropriate built- in exception like ValueError or TypeError or similar, than to subclass from Exception itself. > * Using > Exception is typically a bad. More specific the better. Yes, you should always try to catch the specific exceptions you care about: # Best except ValueError, OverflowError, ZeroDivisionError: # Not so good except Exception: # Worst except: Don't use the last one, except maybe in the interactive interpreter, since it will catch *everything*, even exceptions that probably shouldn't be caught like KeyboardInterrupt. > * Exceptions > should never fail silently. (Should exceptions always be logged?) Certainly not. Exceptions should fail silently if you don't care about them. For example, when connecting to a website, there are many temporary errors that can occur. The best way to handle them is to catch the exception, sleep for a little while, then try again. You need only care if repeated attempts to connect, with larger and larger sleeps, continue to fail. Of course, you might have a debug mode that logs all of these, but if your web browser logged every single time a webpage was slow to respond, you would soon run out of disk space :-) *Errors* should never fail silently, unless explicitly silenced. But an error isn't an error if you don't care about it, and an exception is not necessarily an error. This is an error, because converting a number to uppercase cannot possibly mean anything: mystring = 42 mystring.upper() This is not necessarily an error, since "the list is empty" could be a legitimate situation: mylist = [] first = mylist[0] In this case, it may be appropriate to catch the exception, and either silently swallow it, or do something else. > Best site I have found for exceptions (hopefully this helps someone): * > http://c2.com/cgi/wiki?ExceptionPatterns I haven't read that page for a long time, but as I recall the c2.com website, a lot of the ideas there are better suited to Java and C/C++ (and occasionally Lisp) rather than Python. But still, a valuable (if often confusing) resource. > I'd be interested in hearing others thoughts on this topic with regards > to best practices for when to use exceptions, and when to avoid using > exceptions. The try part of a try...except is *very* fast to set up. It's about as fast as a "pass" (do nothing), so it has little overhead. On the other hand, actually *catching* an exception is quite heavy. So code that catches lots and lots of exceptions may be slow. In that case, it may be faster to "look before you leap" and test ahead of time: # this is faster if most lists are not empty try: process(mylist[0]) except IndexError: handle_empty_list() # this is faster if many lists are empty if mylist: process(mylist[0]) else: handle_empty_list() Only profiling your data can tell you which you should use. On the other hand, here you should *always* use try...except: try: myfile = open("name") except IOError: handle_error() Because this code is wrong: if os.path.exists("name"): myfile = open("name") else: handle_error() It's wrong for a couple of reasons: - just because the file exist
Help regarding urllib
Hello All, I am simply fetching data from robots.txt of a url. Below is my code. siteurl = siteurl.rstrip("/") -- http://mail.python.org/mailman/listinfo/python-list
Re: Help regarding urllib
On Saturday, August 24, 2013 4:15:01 PM UTC+5:30, malhar vora wrote: > Hello All, > > > > > > I am simply fetching data from robots.txt of a url. Below is my code. > > > > siteurl = siteurl.rstrip("/") Sorry for last complete. It was sent by mistake. Here is my code. siteurl = siteurl.rstrip("/") roboturl = siteurl + r'/robots.txt' robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given url print robotdata In above code siteurl is fetched simply from local text file. Whenever I run above code. In place of "/" before robots.txt, it writes "\\" in url as I found in error. The error is given below. This is main function Main URL : www.bestrecipes.com.au $$:www.bestrecipes.com.au ###-->www.bestrecipes.com.au/robots.txt Traceback (most recent call last): File "dataintegrator.py", line 104, in main() File "dataintegrator.py", line 81, in main print "Sitemap Url : " + getSiteMapUrl(i) File "D:\Malhar Data\Projects\Data Parsing\My Code\Final Part\libs\datareader. py", line 50, in getSiteMapUrl robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given ur l File "C:\Python26\lib\urllib.py", line 87, in urlopen return opener.open(url) File "C:\Python26\lib\urllib.py", line 203, in open return getattr(self, name)(url) File "C:\Python26\lib\urllib.py", line 461, in open_file return self.open_local_file(url) File "C:\Python26\lib\urllib.py", line 475, in open_local_file raise IOError(e.errno, e.strerror, e.filename) IOError: [Errno 2] The system cannot find the path specified: 'www.bestrecipes.c om.au\\robots.txt' I am new to Python and not able to figure out this problem. Please help me. Thank you, Malhar Vora -- http://mail.python.org/mailman/listinfo/python-list
Re: Python variable as a string
Thank you all for the reply. Actually yes this was a confusing question, and borne out of trying to make a shortcut. I didnt ask to convert the contents of var into a string. All I needed was to get the literal equivalent "var" because I needed to use it in another dict object - whose keys i named the same (eg 'var') for convenience. Instead i ended up complicating stuff. I resolved this by doing things differently with (if - elif - else). Sorry for the confusion - but thanks all for answering - i can use a code or two of what you have shared! On Fri, Aug 23, 2013 at 11:23 PM, Neil Cerutti wrote: > On 2013-08-23, Jake Angulo wrote: > > I have a list *var* which after some evaluation I need to refer > > to *var* as a string. > > You must make a str version of var. > > > Pseudocode: > > > > var = ['a', 'b' , 'c' , 'd'] > > adict = dict(var='string', anothervar='anotherstring') > > anotherdict = dict() > > if : > > anotherdict[akey] = adict['var'] > > anotherdict[akey] = adict[str(var)] > > Will actually work, though you might prefer: > > anotherdict[akey] = adict[''.join(var)] > > Try them out and see. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to send broadcast IP address to network?
On 23-8-2013 14:32, lightai...@gmail.com wrote: > I want to send a broadcast packet to all the computers connected to my home > router. > > The following 2 lines of code do not work; > host="192.168.0.102" > s.connect((host, port)) > > Can someone advise? > > Thank you. > Use UDP (datagram) sockets. Use sendto() and not connect(), because UDP is connectionless. This should work: import socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) sock.sendto(b"thedata", 0, ("", ))# = port sock.close() Irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast conversion of numbers to numerator/denominator pairs
Steven D'Aprano wrote: > I have a need to convert arbitrary non-complex numbers into numerator/ > denominator pairs. Numbers could be ints, floats, Fractions or Decimals. > For example: > > 2 => (2, 1) > 0.25 => (1, 4) > Fraction(2, 3) => (2, 3) > Decimal("0.5") => (1, 2) > > > The first three cases are easy and fast: > > # ints and Fractions > number.numerator, number.denominator > > # floats are a little slower > number.as_integer_ratio() > > > But Decimals are unfortunately slower. MUCH slower, about 40 times slower > than Fractions in Python 3.3: > > tmp = Fraction.from_decimal(number) > (tmp.numerator, tmp.denominator) > > > This ends up being the bottleneck in my code: once you include the > scaffolding code to select the right conversion method, processing a > large list of Decimals is about fifty times slower than large lists of > floats or fractions. > > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > > which is faster, but not fast enough. Any suggestions? Maybe these micro-optimisations will be sufficient: _trans = bytes.maketrans(bytes(range(10)), b"0123456789") def convert(d): sign, digits, exp = d.as_tuple() num = int(bytes(digits).translate(_trans)) if sign: num = -num return num, 10**-exp You can get the "simplest form" with co-prime numerator and denominator by dividing by fractions.gcd(), but that will of course slow down things. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help regarding urllib
malhar vora wrote: > On Saturday, August 24, 2013 4:15:01 PM UTC+5:30, malhar vora wrote: >> Hello All, >> >> >> >> >> >> I am simply fetching data from robots.txt of a url. Below is my code. >> >> >> >> siteurl = siteurl.rstrip("/") > > Sorry for last complete. It was sent by mistake. > > Here is my code. > > siteurl = siteurl.rstrip("/") > roboturl = siteurl + r'/robots.txt' > robotdata = urllib.urlopen(roboturl).read() # Reading robots.txt of given url > print robotdata > > In above code siteurl is fetched simply from local text file. Why aren't you showing us what is in that local text file? Or more specifically what siteurl turns out to be? I suspect it's missing the http:// prefix > IOError: [Errno 2] The system cannot find the path specified: > 'www.bestrecipes.c > om.au\\robots.txt' > Looks to me like it decided this url referred to a file. That's the default behavior when you don't specify the scheme identifier (eg. 'http") Also it might well have been necessary to specify what Python version and OS you're running this on. For example, the single backslash character is specific to Windows. (The doubling presumably is an artifact of how the error message is displayed, eg. look at how repr() displays strings) -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: can't get utf8 / unicode strings from embedded python
On Sat, Aug 24, 2013, at 2:45, David M. Cotter wrote: > > you need to use u" ... " delimiters for Unicode, otherwise the results you > > get are completely arbitrary and depend on the encoding of your terminal. > okay, well, i'm on a mac, and not using "terminal" at all. but if i > were, it would be utf8 > but it's still not flying :( > so, it seems that i AM getting my utf8 bytes, but i'm getting them > converted to macRoman. huh? where is macRoman specified, and how to i > change that to utf8? i think that's the missing golden ticket You say you're not using terminal. What _are_ you using? -- http://mail.python.org/mailman/listinfo/python-list
Re: python interface to iMacros
On 08/23/2013 09:13 AM, inq1ltd wrote: > Python help, > > I am running iMacros from linux/firefox > and doing most of what I want. > > But, there are times when I want to do > something of the net and then back > to the iMacros script. > > Are there any projects out there > that will connect python to imacros, > something on the order of pexpect? I have no idea really, but you can drive Firefox using a thing called selenium. http://docs.seleniumhq.org/ There is a python library for interfacing with selenium, and from that you can drive firefox from python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
On 24/08/2013 11:27, Steven D'Aprano wrote: On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [snip] * Using Exception is typically a bad. More specific the better. Yes, you should always try to catch the specific exceptions you care about: # Best except ValueError, OverflowError, ZeroDivisionError: That should be: except (ValueError, OverflowError, ZeroDivisionError): # Not so good except Exception: # Worst except: [snip] -- http://mail.python.org/mailman/listinfo/python-list
Re: can't get utf8 / unicode strings from embedded python
> What _are_ you using? i have scripts in a file, that i am invoking into my embedded python within a C++ program. there is no terminal involved. the "print" statement has been redirected (via sys.stdout) to my custom print class, which does not specify "encoding", so i tried the suggestion above to set it: static const char *s_RedirectScript = "import " kEmbeddedModuleName "\n" "import sys\n" "\n" "class CustomPrintClass:\n" " def write(self, stuff):\n" " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" "class CustomErrClass:\n" " def write(self, stuff):\n" " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" "sys.stdout = CustomPrintClass()\n" "sys.stderr = CustomErrClass()\n" "sys.stdout.encoding = 'UTF-8'\n" "sys.stderr.encoding = 'UTF-8'\n"; but it didn't help. I'm still getting back a string that is a utf-8 string of characters that, if converted to "macRoman" and then interpreted as UTF8, shows the original, correct string. who is specifying macRoman, and where, and how do i tell whoever that is that i really *really* want utf8? -- http://mail.python.org/mailman/listinfo/python-list
how to read mixed from multiple csv file
Hi, My 20 csv files has string header, and first two columns are string (e.g., 1999-01-02, 01:00:00) among the 50 columns. Other columns store numerical values (int, or float) I need to do data analysis for these data. For example, extract the each month data from each of the cvs files (each csv file stores 1 year data) and there are 20 year data. in addition, I want to store the data in disk so that I can retrieve data quickly, just like save and load in Matlab. Currently, I use structured array data = [] i = 0 for s in range(1991, 2011): fileName = folder +_{_sY}0101_{_sY}1231_725300.csv".format(_sY=s) data.append(np.genfromtxt(fileName, delimiter=",", dtype=None, names=True)) i += 1 np.save("alldata", data) However, when I load data "np.load("alldata.npy")", it is becomes 0-d array which is different from original one. My question is that (1) How to store or save the data? (2) as you can see, I use list to store all the 20 ndarrays, I do not feel it is a good way. Is there any suggestion for the data structure I should use? Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: can't get utf8 / unicode strings from embedded python
Le samedi 24 août 2013 18:47:19 UTC+2, David M. Cotter a écrit : > > What _are_ you using? > > i have scripts in a file, that i am invoking into my embedded python within a > C++ program. there is no terminal involved. the "print" statement has been > redirected (via sys.stdout) to my custom print class, which does not specify > "encoding", so i tried the suggestion above to set it: > > > > static const char *s_RedirectScript = > > "import " kEmbeddedModuleName "\n" > > "import sys\n" > > "\n" > > "class CustomPrintClass:\n" > > " def write(self, stuff):\n" > > " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" > > "class CustomErrClass:\n" > > " def write(self, stuff):\n" > > " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" > > "sys.stdout = CustomPrintClass()\n" > > "sys.stderr = CustomErrClass()\n" > > "sys.stdout.encoding = 'UTF-8'\n" > > "sys.stderr.encoding = 'UTF-8'\n"; > > > > > > but it didn't help. > > > > I'm still getting back a string that is a utf-8 string of characters that, if > converted to "macRoman" and then interpreted as UTF8, shows the original, > correct string. who is specifying macRoman, and where, and how do i tell > whoever that is that i really *really* want utf8? Always encode a "unicode" into the coding of the "system" which will host it. Adapting the hosting system to your "unicode" (encoded unicode) is not a valid solution. A non sense. sys.std***.encodings do nothing. They only give you information about the coding of the hosting system. The "system" can be anything, a db, a terminal, a gui, ... Shortly, your "writer" should encode your "stuff" to your "host" in a adequate way. It is up to you to manage coherence. If your passive "writer" support only one coding, adapt "stuff", if "stuff" lives in its own coding (due to c++ ?) adapt your "writer". Example from my interactive interpreter. It is in Python 3, not important, basically the job is the same in Python 2. This interpreter has the capability to support many codings, and the coding of this host system can be changed on the fly. A commented session. By default, a string, type str, is a unicode. The host accepts "unicode". So, by default the sys.stdout coding is ''. >>> sys.stdout.encoding = '' >>> print("frøânçïé") frøânçïé >>> Setting the host to utf-8 and printing the above string gives "something", but encoding into utf-8 works fine. >>> sys.stdout.encoding = 'utf-8' >>> sys.stdout.encoding 'utf-8' >>> print("frøânçïé") frøânçïé >>> print("frøânçïé".encode('utf-8')) 'frøânçïé' Setting the host to 'mac-roman' works fine too, as long it is properly encoded! >>> sys.stdout.encoding = 'mac-roman' >>> print("frøânçïé".encode('mac-roman')) 'frøânçïé' But >>> print("frøânçïé".encode('utf-8')) 'fr√∏√¢n√ß√Ø√©' Ditto for cp850 >>> sys.stdout.encoding = 'cp850' >>> print("frøânçïé".encode('cp850')) 'frøânçïé' If the repertoire of characters of a coding scheme does not contain the characters -> replace >>> sys.stdout.encoding = 'cp437' >>> print("frøânçïé".encode('cp437')) Traceback (most recent call last): File "", line 1, in File "c:\python32\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character '\xf8' in position 2: character maps to >>> print("frøânçïé".encode('cp437', 'replace')) 'fr?ânçïé' Curiousities >>> sys.stdout.encoding = 'utf-16-be' >>> print("frøânçïé") f r ø â n ç ï é >>> print("frøânçïé".encode('utf-16-be')) 'frøânçïé' >>> sys.stdout.encoding = 'utf-32-be' >>> print("frøânçïé".encode('utf-32-be')) 'frøânçïé' jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: python interface to iMacros
> On 08/23/2013 09:13 AM, inq1ltd wrote: > > Python help, > > > > I am running iMacros from > > linux/firefox and doing most of > > what I want. > > > > But, there are times when I want to > > do something of the net and then > > back to the iMacros script. > > > > Are there any projects out there > > that will connect python to imacros, > > something on the order of pexpect? > > I have no idea really, but you can > drive Firefox using a thing called > selenium. > http://docs.seleniumhq.org/ > > There is a python library for > interfacing with selenium, and from > that you can drive firefox from > python. I've looked at the site. Sounds like something I'll try. Thanks for the reply. jimonlinux -- http://mail.python.org/mailman/listinfo/python-list
Re: can't get utf8 / unicode strings from embedded python
On Sat, Aug 24, 2013 at 9:47 AM, David M. Cotter wrote: > > > What _are_ you using? > i have scripts in a file, that i am invoking into my embedded python within a > C++ program. there is no terminal involved. the "print" statement has been > redirected (via sys.stdout) to my custom print class, which does not specify > "encoding", so i tried the suggestion above to set it: > > static const char *s_RedirectScript = > "import " kEmbeddedModuleName "\n" > "import sys\n" > "\n" > "class CustomPrintClass:\n" > " def write(self, stuff):\n" > " " kEmbeddedModuleName "." kCustomPrint "(stuff)\n" > "class CustomErrClass:\n" > " def write(self, stuff):\n" > " " kEmbeddedModuleName "." kCustomErr "(stuff)\n" > "sys.stdout = CustomPrintClass()\n" > "sys.stderr = CustomErrClass()\n" > "sys.stdout.encoding = 'UTF-8'\n" > "sys.stderr.encoding = 'UTF-8'\n"; > > > but it didn't help. > > I'm still getting back a string that is a utf-8 string of characters that, if > converted to "macRoman" and then interpreted as UTF8, shows the original, > correct string. who is specifying macRoman, and where, and how do i tell > whoever that is that i really *really* want utf8? > -- If you're running this from a C++ program, then you aren't getting back characters. You're getting back bytes. If you treat them as UTF-8, they'll work properly. The only thing wrong is the text editor you're using to open the file afterwards- since you aren't specifying an encoding, it's assuming MacRoman. You can try putting the UTF-8 BOM (it's not really a BOM) at the front of the file- the bytes 0xEF 0xBB 0xBF are used by some editors to identify a file as UTF-8. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
On 8/24/2013 6:27 AM, Steven D'Aprano wrote: On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: [...] * Seems like exception handing within Classes is largely avoided and is typically only used when calling external libraries. There is certainly no rule "avoid exceptions inside classes". Methods often raise exceptions to signal an error, e.g.: "My string".index("spam") The rule only makes sense if it referring to try: except: in top-level class code, outside of def statements. Even then, it is wrong. ... # Worst except: Don't use the last one, except maybe in the interactive interpreter, Stick with never. "except:" means the same thing as "except BaseException:", except that the latter indicates a deliberate choice rather than an indication of carelessness or laziness. A bare except: is a disservice to the next maintainer of the code. since it will catch *everything*, even exceptions that probably shouldn't be caught like KeyboardInterrupt. In Idle, when you type 'expression(' and hesitate, Idle tries to evaluate 'expression' to a function, behind the scenes, in order to provide a calltip with the function signature. Any error in 'eval(expression)' should be caught and ignored so the user can continue typing. This is one place where the original authors were too specific. They only caught NameError and AttributeError, when those were not the only possible eval errors in practice. The result was that people would occasionally type '(' and see idle quit. idlelib .py files have about 20 bare 'except:'s, which I will try to fill out when I have reviewed the try part and understand what should be caught. It would be easier for me to read the code if the original authors had added their best guess as to what should be expected and caught. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Using PyQT with QT Designer
Thanks. I probably will do exactly like you suggested later on. But, those two lines have solved the problem I had and I can work on the actual program now. I can come back to the GUI later. Here is what it looks like now: http://i.imgur.com/sLiSU6M.png On Friday, August 23, 2013 7:35:53 PM UTC-5, Lee Harr wrote: > > That's the problem though. It is exactly how I want it in designer. It's > > perfect as it is in designer when I preview it. Here is a screenshot of the > > preview: http://i.imgur.com/ULRolq8.png > > That's not a preview. That's just the regular design view. > (you can tell by the little dots in the background) > > You need to go to Form -> Preview... to see the actual preview. > > That said... > > 1.) You may want to ask your question on the PyQt mailing list. Though > you are talking with the undisputed PyQt expert in Phil, there are more > people on the other list who are familiar with PyQt and who may be willing > to look more closely at your specific code. > > 2.) It may be that the examples you are looking at are not sufficient to > help you with the situation you are in. For instance, I've written several > programs using Designer and PyQt and I would recommend against > using the pyuic method. > > When I first started with PyQt I also used pyuic and eventually I found > the PyQt4.uic method works better for me. > > 3.) Layouts. You have to use them with Qt or you're going to have a > bad time. > > Looking at your design, I would do something like ... > > - select the two buttons on the left and click "Lay Out Vertically" > - select the two large white boxes and click "Lay Out Vertically" > - put a vertical spacer underneath the red X button > - select the red button and the spacer and click "Lay Out Vertically" > - at this point you may need to resize and rearrange your three vertical > layouts so that they don't overlap and are in approximately the positions > that you want, then > - select the main window and click "Lay Out Horizontally" > > Something along those lines would get you about to where you want > to be. The form may not look _exactly_ the way you have it there, but > it will be a more flexible design and nothing will be overlapping. -- http://mail.python.org/mailman/listinfo/python-list
Help in Nltk
Can anyone help me for the tasks below in nltk 1. The system mustdemonstrate false positiveand false negativeexamples using any stemmer (Task 1.1) 2. The system mustdemonstrate the differences between successive layers of the Porter stemmer through a couple of examples. (Task 1.2). 3. The system mustbe ableto automatically detect whether a textis in French or English and stemit in the detected language. The stemmer for French willonlybe removingplurals.(Tasks 2 and 3). 4. The system mustdemonstrate the use of the Brill Tagger (Task 4.2). 5. The system mustdemonstrate the use of POS tags for information extraction to extract all types of fishand all types of trees mentioned on the Wikipedia pages for Fishand Tree (Task 6) 6. The system mustdemonstrate the use of WordNet to expand nouns with their synonymsand their hypernyms (Task 9) -- http://mail.python.org/mailman/listinfo/python-list
New way of writing socket servers in #Linux kernel 3.9 (and in #Python too)
This may be of interest to readers of this newsgroup: Original article: http://lnkd.in/taAFNt Content (without links): A new way of writing socket servers has been introduced with the Linux kernel 3.9. It involves the ability to bind multiple listening sockets to the same port on the same host, by unrelated processes or threads. The socket option is called SO_REUSEPORT. Article about SO_REUSEPORT. The above article includes a demo of how to use the feature in Python, using the example of a simple echo server and netcat. Another article about SO_REUSEPORT on LWN.net by Michael Kerrisk. Hacker News thread about SO_REUSEPORT. About Michael Kerrisk. He has worked at DEC (Digital Equipment Corporation) and Google in the past. Coincidentally, I happened to have come across Michael Kerrisk's Unix and Linux work some months earlier and had emailed him with a few questions about it, which he was kind enough to answer. Kerrisk is the author of the book The Linux Programming Interface, which seems like an interesting and useful book. >From the web site for the The Linux Programming Interface book: [ The Linux Programming Interface (published in October 2010, No Starch Press, ISBN 978-1-59327-220-3) is a detailed guide and reference for Linux and UNIX system programming. With 1552 pages, 115 diagrams, 88 tables, nearly 200 example programs, and over 200 exercises, TLPI is the most comprehensive description of Linux and UNIX system programming available. The author, Michael Kerrisk, is the maintainer of the Linux man-pages project, which documents the Linux kernel and glibc APIs. He has long been active in the documentation, testing, and design review of Linux kernel-userspace interfaces. ] And if you want to write command-line programs in Linux using C (an area closely related to the topic of the TLPI book), you may wish to check out my article on the subject, written for IBM developerWorks: Developing a Linux command-line utility. I have not yet tried out the SO_REUSEPORT option, because I need to get Linux kernel 3.9 first, but it seems like a useful technique for increasing performance of socket servers. Note that there are various other issues involved, so you may not get increased performance just by using this option in your code. As always with performance tuning, you have to profile your code, identify hotspots, and then only work on improving certain parts of it that seem to be the bottlenecks. And in this case, even before all that, you may need to evaluate whether this socket option is relevant to your application at all. So, caveat lector :-) - Vasudev Ram - Dancing Bison Enterprises -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast conversion of numbers to numerator/denominator pairs
On 24 August 2013 13:30, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > > def convert(d): > sign, digits, exp = d.as_tuple() > num = int(''.join([str(digit) for digit in digits])) > if sign: num = -num > return num, 10**-exp > > which is faster, but not fast enough. Any suggestions? > Straightforward multiply and add takes about 60% of the time for a single digit on my machine compared to the above, and 55% for 19 digits (so reasonably consistent). It's about 10x slower than fractions. def convert_muladd(d, _trans=_trans, bytes=bytes): sign, digits, exp = d.as_tuple() num = 0 for digit in digits: num *= 10 num += digit if sign: num = -num return num, 10**-exp Breakdown of the above (for 19 digits): d.as_tuple() takes about 35% of the time. The multiply and add takes about 55% of the time. The exponentiation takes about 10% of the time. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list
Re: Fast conversion of numbers to numerator/denominator pairs
On 25 August 2013 07:59, Tim Delaney wrote: > Breakdown of the above (for 19 digits): > > d.as_tuple() takes about 35% of the time. > > The multiply and add takes about 55% of the time. > > The exponentiation takes about 10% of the time. > Bah - sent before complete. Since the multiply and add takes such a significant proportion of the time, compiling the above with Cython should gain you a big win as well. Or find some other way to turn that loop into native code. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
On Sat, 24 Aug 2013 15:57:55 -0400, Terry Reedy wrote: >> # Worst >> except: >> >> Don't use the last one, except maybe in the interactive interpreter, > > Stick with never. "except:" means the same thing as "except > BaseException:", except that the latter indicates a deliberate choice > rather than an indication of carelessness or laziness. > > A bare except: is a disservice to the next maintainer of the code. Do you know anyone who maintains code typed in the interactive interpreter? :-) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling Practices / Patterns
Hi Steven, Yea this is great. Thanks for the feedback. On Saturday, August 24, 2013 3:27:45 AM UTC-7, Steven D'Aprano wrote: > On Fri, 23 Aug 2013 22:25:55 -0700, snarf wrote: > > > > [...] > > > * Seems like exception handing within Classes is largely avoided and is > > > typically only used when calling external libraries. > > > > There is certainly no rule "avoid exceptions inside classes". Methods > > often raise exceptions to signal an error, e.g.: > > > > "My string".index("spam") > > > > > > Less common, methods can raise exceptions as part of their flow control. > > The most obvious example is StopIteration, used by iterators and often by > > __iter__ or next methods. > > > > > > > * Try/Except type > > > statements seem to be used more within modules, main functions, wrapper > > > scripts. > > > > It depends on whose code you are reading. I don't write a lot of classes, > > but when I do, I often use try...except inside them. > > > > If try...except gets used more frequently in module's top level, it is > > because the sort of things that you do at the top level often needs > > exception handling. For example, you might have a fallback module: > > > > try: > > import this_module > > except ImportError: > > import that_module as this_module > > > > > > You will very rarely see that inside a class, since you very rarely > > import modules inside a class. > > > > > > > * Classes should be coded in a way that exceptions > > > > I think you forgot to finish the sentence. > > > > > > > * Better to > > > never write your own exceptions (unless you absolutely have to). > > > > That depends. > > > > On the one hand, nobody wants a million different exception types. On the > > other hand, nobody wants just *one* exception type, and no way to > > distinguish between different kinds of errors. Somewhere between one and > > one million is an appropriate number of exception types. > > > > The right answer is to be conservative about creating new exceptions, but > > don't be scared to create one when you need one. > > > > But when you do, it is often better to subclass from an appropriate built- > > in exception like ValueError or TypeError or similar, than to subclass > > from Exception itself. > > > > > > > * Using > > > Exception is typically a bad. More specific the better. > > > > Yes, you should always try to catch the specific exceptions you care > > about: > > > > > > # Best > > except ValueError, OverflowError, ZeroDivisionError: > > > > > > # Not so good > > except Exception: > > > > > > # Worst > > except: > > > > > > Don't use the last one, except maybe in the interactive interpreter, > > since it will catch *everything*, even exceptions that probably shouldn't > > be caught like KeyboardInterrupt. > > > > > > > * Exceptions > > > should never fail silently. (Should exceptions always be logged?) > > > > Certainly not. Exceptions should fail silently if you don't care about > > them. For example, when connecting to a website, there are many temporary > > errors that can occur. The best way to handle them is to catch the > > exception, sleep for a little while, then try again. You need only care > > if repeated attempts to connect, with larger and larger sleeps, continue > > to fail. > > > > Of course, you might have a debug mode that logs all of these, but if > > your web browser logged every single time a webpage was slow to respond, > > you would soon run out of disk space :-) > > > > *Errors* should never fail silently, unless explicitly silenced. But an > > error isn't an error if you don't care about it, and an exception is not > > necessarily an error. > > > > This is an error, because converting a number to uppercase cannot > > possibly mean anything: > > > > mystring = 42 > > mystring.upper() > > > > > > This is not necessarily an error, since "the list is empty" could be a > > legitimate situation: > > > > mylist = [] > > first = mylist[0] > > > > In this case, it may be appropriate to catch the exception, and either > > silently swallow it, or do something else. > > > > > > > Best site I have found for exceptions (hopefully this helps someone): * > > > http://c2.com/cgi/wiki?ExceptionPatterns > > > > I haven't read that page for a long time, but as I recall the c2.com > > website, a lot of the ideas there are better suited to Java and C/C++ > > (and occasionally Lisp) rather than Python. But still, a valuable (if > > often confusing) resource. > > > > > > > > > I'd be interested in hearing others thoughts on this topic with regards > > > to best practices for when to use exceptions, and when to avoid using > > > exceptions. > > > > The try part of a try...except is *very* fast to set up. It's about as > > fast as a "pass" (do nothing), so it has little overhead. >
Re: Exception Handling Practices / Patterns
Appreciate the feedback. I was hoping to get as much perspective as possible. On Saturday, August 24, 2013 12:18:59 AM UTC-7, Dave Angel wrote: > snarf wrote: > > > > > Greetings, > > > > > > As I tread through my journey of OO I am trying to determine if there is a > > good approach for exception handling within classes. > > > > > > From my readings and gatherings - it seems I have found a common theme, but > > I am trying to solicit from the experts. > > > > > > Here is what I have found (I may be restating the obvious so please forgive > > me in advance): > > > > > > * Seems like exception handing within Classes is largely avoided and is > > typically only used when calling external libraries. > > > * Try/Except type statements seem to be used more within modules, main > > functions, wrapper scripts. > > > > Exceptions are used when useful. I don't see any bias towards any one > > location. > > > > > * Classes should be coded in a way that exceptions > > > > You seem to be missing the last part of this sentence. > > > > > * Better to never write your own exceptions (unless you absolutely have to). > > > > If you mean to avoid writing exception classes, then I say nonsense. > > Just derive them from the closest meaningful exception class, so that a > > user can combine handlers when reasonable. > > > > > * Using Exception is typically a bad. More specific the better. > > > > If you mean in an except statement, then I'd agree. > > > > > * Exceptions should never fail silently. (Should exceptions always be > > logged?) > > > > Exceptions should be caught if you can handle them, or if you need to > > convert them to a different exception that someone further up the stack > > can handle. Sometimes handling means do nothing. > > > > > > > > Best site I have found for exceptions (hopefully this helps someone): > > > * http://c2.com/cgi/wiki?ExceptionPatterns > > > > But that's for Java. java is not C++, and neither is it Python. For > > one thing, Python exception overhead is deliberately much less, and they > > are used more freely. > > > > Notice that exceptions are used to terminate for loops, and that's a > > *normal* exit to the loop. They also appear in other places under the > > covers. Don't be afraid of them. > > > > -- > > DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda function Turing completeness
Musical Notation writes: > Is it possible to write a Turing-complete lambda function (which does > not depend on named functions) in Python? The wording of this question is questionable. Turing completeness is not an attribute of a function, but of a system (for example a programming language or a machine). It means that for every Turing machine you can write a program in that language or program the machine in such a way that it emulates that Turing machine. So you could ask if the subset of the Python programs consisting only of a lambda expression is Turing complete. Or alternatively if for every Turing machine you can write a lambda expression that emulates that Turing machine. It has been proven that the λ calculus is equivalent to Turing machines, so if the lambda calculus can be emulated with Python's lambda expressions the answer is yes. In the lambda calculus you can define lambda expressions and apply functions to parameters. The parameters may be functions (in fact in the pure λ calculus there is nothing else), so functions must be first class citizens. Fortunately in Python this is the case. So we suspect it can be done. An important result in the λ calculus is that every expression can be expressed in three functions S, K and I with only function application. So we are going to try to do these in Python and see if it works. The definitions in the λ calculus are: S = λ x y z. (x z) (y z) K = λ x y. x I = λ x. x The dot is used where Python uses :, function application is written as juxtaposition: f x and λ x y is an abbreviation of λ x. λ y So we are going to translate these in python. We have to explicitely write the lambda's (each one with a single parameter) and add parentheses around the function arguments if not already there. >>> S = lambda x: lambda y: lambda z: x(z) (y(z)) >>> K = lambda x: lambda y: x >>> I = lambda x: x Now there is a theorem that SKK == I (I is the identity), so we are going to test that: >>> S(K)(K)('test') 'test' a few more tests: >>> for x in range(100): ... if S(K)(K)(x) != I(x): ... print('Not equal for x = %s' % x) ... All seem to be equal. Of course we still have used names for the functions, but this is not essential. We can just replace each of S, K, and I with their definition: >>> print((lambda x: lambda y: lambda z: x(z) (y(z))) # S ... (lambda x: lambda y: x) # (K) ... (lambda x: lambda y: x)('test'))# (K) ('test') test >>> for x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': ... if ((lambda x: lambda y: lambda z: x(z) (y(z))) ... (lambda x: lambda y: x) ... (lambda x: lambda y: x)(x)) != (lambda x: x)(x): ... print('Not equal for x = %s' % x) ... Success! Now the pure λ lambda calculus has to express inter=gers, booleans etc. also as lambda expressions and this makes it really unwieldy. However, you can add some standard functions or expressions for these and that doesn't diminish the expressiveness of the calculus. So I suppose that you want to allow the use of all standard Python functions and expressions. Modern Pythons have conditional expressions, so this helps. We don't have to emulate booleans and conditions with weird lambda expressions. In Python's lambda expressions you can not use statements, only expressions, so without conditional expressiosn Python's booleans wouldn't be very useful. The remaining problem is how to use loops or recursion. I'll do that in a separate posting. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- http://mail.python.org/mailman/listinfo/python-list
Re: can't get utf8 / unicode strings from embedded python
On Sat, Aug 24, 2013, at 12:47, David M. Cotter wrote: > > What _are_ you using? > i have scripts in a file, that i am invoking into my embedded python > within a C++ program. there is no terminal involved. the "print" > statement has been redirected (via sys.stdout) to my custom print class, > which does not specify "encoding", so i tried the suggestion above to set > it: That doesn't answer my real question. What does your "custom print class" do with the text? -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda function Turing completeness
This is the second part of my posting on the Turing completeness of Python's lambda expressions. This time I am going to define a recursive function as a lambda expression (I use lambda when I am talking about Python's lambda expressions, and λ for the theory – λ calculus.) Now of course it is easy to define a recursive function if you can use its function name inside the body. But the question of the OP was if you can do it without named functions. The pure λ calculus only works with unnamed λ expressions. Therefore we need a special operator to define recursive functions. This is the so called Y combinator, or Y operator[1]. The defining characteristic of Y is: Y(f) = f(Y(f)) for all functions f. There are several possible definitions of this operator, but some of them work only for programming languages with lazy evaluation or call by name. For Python's call by valye the following one will work: Y = λf.(λx.f (λv.((x x) v))) (λx.f (λv.((x x) v))) Translated in Python: >>> Y = lambda f: (lambda x: f (lambda v: ((x (x)) (v \ ... (lambda x: f (lambda v: ((x (x)) (v We are going to define a lambda expression for the factorial function. We need a helper function for this. The idea is to have the final recursive function as a parameter of the helper function. See [1]. def fact_helper(f, n): if n == 0: return 1 else: return n * f(n-1) No we have to rewrite this to get a proper lambda expression. We split the two parameters and give each of them a lambda, and we replace the if statement with a conditional expression. >>> fact_helper = lambda f: lambda n: (1 if n == 0 else n * f(n-1)) Now we apply the Y combinator to fact_helper to get the recursive fact function and check it: >>> fact = Y (fact_helper) >>> fact(5) 120 Of course to get pure we have to get rid of the names of the functions. So we replace each of Y, fact and fact_helper with their definition: >>> (lambda f: (lambda x: f (lambda v: ((x (x)) (v \ ...(lambda x: f (lambda v: ((x (x)) (v) \ ...(lambda f: lambda n: (1 if n == 0 else n * f(n-1))) (5) 120 Lo and behold! We have the right answer. Now writing a universal Turing machine as a single Python lambda expression is left as an exercise for the reader. BTW. If you use Python 3 you can have print inside a lambda expression, so this makes all this even nicer. -- [1] http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- http://mail.python.org/mailman/listinfo/python-list
Re: Setting the value of True
Steven D'Aprano wrote: As for why None, True and False are treated differently than built-ins, if I remember the reason why, it is because they are considered fundamental to the inner workings of Python, unlike mere builtins like len, map, etc. and therefore should be protected. It's probably more to permit optimisation. Treating them as constants avoids a name lookup every time they're used. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too)
#Linux, #Python? This this hash tag stuff is getting out of hand, don't you think? -- http://mail.python.org/mailman/listinfo/python-list
List getting extended when assigned to itself
Hi Python Friends, I came across an example which is as below, >>> var = [1, 12, 123, 1234] >>> var [1, 12, 123, 1234] >>> var[:0] [] >>> var[:0] = var >>> var [1, 12, 123, 1234, 1, 12, 123, 1234] >>> Here in var[:0] = var we are assigning an entire list to the beginning of itself. So shouldn't it be something like, [[1, 12, 123, 1234], 1, 12, 123, 1234] It happens when we do the below, >>> var = [1, 12, 123, 1234] >>> var[0] = var >>> var [[...], 12, 123, 1234] >>> Literally var[0] = var and var[:0] = var almost meens the same. But why is the difference in output? Can anyone explain what happens when slicing assignment and direct assignment. Regards, Krishnan -- http://mail.python.org/mailman/listinfo/python-list
Re: List getting extended when assigned to itself
On Sat, Aug 24, 2013 at 8:52 PM, Krishnan Shankar wrote: > Hi Python Friends, > > I came across an example which is as below, > var = [1, 12, 123, 1234] var > [1, 12, 123, 1234] var[:0] > [] var[:0] = var var > [1, 12, 123, 1234, 1, 12, 123, 1234] > > Here in var[:0] = var we are assigning an entire list to the beginning of > itself. So shouldn't it be something like, > > [[1, 12, 123, 1234], 1, 12, 123, 1234] > > It happens when we do the below, > var = [1, 12, 123, 1234] var[0] = var var > [[...], 12, 123, 1234] > > Literally var[0] = var and var[:0] = var almost meens the same. But why is > the difference in output? Can anyone explain what happens when slicing > assignment and direct assignment. Are you sure they're almost the same? >>> var[0] 1 >>> var[:0] [] One's a list and one is an integer. It's hard for them to be any more different. var[0] means that you should be setting an element of the list. var[:0] says you want to update a piece of the list, not an element inside it. -- http://mail.python.org/mailman/listinfo/python-list
Re: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too)
On Sat, Aug 24, 2013 at 7:08 PM, Michael Torrie wrote: > #Linux, #Python? This this hash tag stuff is getting out of hand, don't > you think? Didn't you hear? In an effort to redefine itself for the modern Internet, Usenet is adding support for hash tags and limiting posts to 140 characters because kids these days just don't want to read anything longer. -- http://mail.python.org/mailman/listinfo/python-list
Re: New way of writing socket servers in #Linux kernel 3.9 (and in #Python too)
On 08/24/2013 10:06 PM, Benjamin Kaplan wrote: > On Sat, Aug 24, 2013 at 7:08 PM, Michael Torrie wrote: >> #Linux, #Python? This this hash tag stuff is getting out of hand, don't >> you think? > > Didn't you hear? In an effort to redefine itself for the modern > Internet, Usenet is adding support for hash tags and limiting posts to > 140 characters because kids these days just don't want to read > anything longer. Sweet. And here I thought it was irc channels. -- http://mail.python.org/mailman/listinfo/python-list
Re: List getting extended when assigned to itself
On Sun, 25 Aug 2013 09:22:27 +0530, Krishnan Shankar wrote: > Hi Python Friends, > > I came across an example which is as below, > var = [1, 12, 123, 1234] var > [1, 12, 123, 1234] var[:0] > [] var[:0] = var var > [1, 12, 123, 1234, 1, 12, 123, 1234] > Here in var[:0] = var we are assigning an entire list to the beginning > of itself. So shouldn't it be something like, > > [[1, 12, 123, 1234], 1, 12, 123, 1234] No, you have misunderstood how slicing works. When you assign to a slice, you *replace* the existing slice with items from the other list: py> L = [1, 2, 3, 4, 5] py> L[2:4] = [None, None, None] py> L [1, 2, None, None, None, 5] Notice that it does not insert the list [None, None, None] as a single item. If the slice you replace is empty, this is equivalent to inserting the items: py> L = [1, 2, 3, 4, 5] py> L[2:2] = [None, None, None] py> L [1, 2, None, None, None, 3, 4, 5] The beginning of the list is just an empty slice: L[:0] means "all the items, starting at the beginning of the list, and finishing *before* index zero". So that makes it an empty slice, and items are inserted after the start of the list but before index zero: py> L = [1, 2, 3, 4, 5] py> L[:0] = [None, None, None] py> L [None, None, None, 1, 2, 3, 4, 5] So assigning to a slice *always* extends. If you try to assign a single value, not inside a list or other iterable, it fails: py> L = [1, 2, 3, 4, 5] py> L[:0] = None Traceback (most recent call last): File "", line 1, in TypeError: can only assign an iterable Remember that indexes in Python are *before* the item, so when slicing, Python cuts at the vertical lines | as shown below: | a | b | c | d | e | f | g | The vertical lines are numbered 0 through 7, and the slice [2:5] cuts as shown: | a | b | c | d | e | f | g | 0---1---2---3---4---5---6---7 [2:5] => | c | d | e | > It happens when we do the below, > var = [1, 12, 123, 1234] var[0] = var var > [[...], 12, 123, 1234] Here you are *not* assigning to a slice, but setting a single item. The item at position 0 is replaced with the contents of var, which happens to be the same list, but that is just a distraction. It is more clear when you use a different value: py> L = [1, 2, 3, 4, 5] py> L[0] = "something" py> L ['something', 2, 3, 4, 5] > Literally var[0] = var and var[:0] = var almost meens the same. No, they are very different. Even though the difference is a single character, var[:index] and var[index] are very different, no matter what the value of index. The first is a slice ending at index, the second is a single item at index. The same naturally applies to var[index:] as well, which is a slice starting at index. If you wish to insert a sequence as a single object, you have two choices: you can use the list insert() method, or you can wrap the sequence in a list, and then use slice assignment: py> L = [1, 2, 3, 4, 5] py> L[2:4] = [L] py> L [1, 2, [...], 5] -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Checking homogeneity of Array using List in Python
I am unable to check homogeneity of Array. I have take Array type Int to be default for my code. Instead of getting Error on NON-INT Values. I want to take input as string. Then check if all input is in (0-9) form, I typecast it into int and Accept. Else, I would like to skip that input. eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242'] I want it to be interpreted as: [1, [None], [None], [None], 43242] NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking homogeneity of Array using List in Python
On Sun, Aug 25, 2013 at 3:50 PM, wrote: > NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance. You'll have to do your own homework, then. Python strongly favours the use of inbuilt functions. ChrisA -- http://mail.python.org/mailman/listinfo/python-list