Re: How to split a list type array into float array?
On Tue, May 21, 2019 at 03:38:43AM -0700, Madhavan Bomidi wrote: > Hi, > > I have the following list type data: > > [' 29.7963 29.6167 29.4371 29.2574 29.0778 28.8982 28.7185 > 28.5389 28.3593 28.1797 28. 27.8204 27.6408 27.4611 27.2815 > 27.1019 26.9223 26.7426 26.5630 26.3834 26.2037 26.0241 > 25.8445 25.6649 25.4852 25.3056 25.1260 24.9463 24.7667 24.5871 > 24.4075 24.2278 24.0482 -0.2616 -0.3215 -0.3813 -0.4412\n'] > > Can anyone help me split as a float array? did you mean s = [' 29.79... ...\n'] f = [float(f) for f in s[0].strip().split()] and start to read the docs? https://docs.python.org/3/library/stdtypes.html?highlight=split#str.split https://docs.python.org/3/tutorial/datastructures.html?highlight=list and so on... a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Querying MariaDB from python
Hi Tony, On Tue, Oct 02, 2018 at 05:07:38PM +0100, Tony van der Hoff wrote: > On 02/10/18 16:47, Ervin Hegedüs wrote: > > hi, > > > > now rows will looks like this: > > ({'id':...,...},{'id':...,}...) > > Thanks Ervin, but: > >cursor = cnx.cursor(pymysql.cursors.DictCursor) > NameError: name 'pymysql' is not defined > > I have been using the mysql.connector module, which seems to be the > "official" python interface. I hadn't spotted the pymysql module. Is the > consensus here that pymysql is the preferred connector? well, since I'm using Python3, I didn't use "old" MySQLdb python module, I switched to pymysql - but as I know they are compatible, so you can use: cnx.cursor(MySQLdb.cursors.DictCursor) I don't know about "mysql.connector" module yet - but it doesn't mean that it doesn't existst :) hth, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Querying MariaDB from python
hi, On Tue, Oct 02, 2018 at 04:14:45PM +0100, Tony van der Hoff wrote: > I'm writing a database application, in python 3,5 under Debian9. > > My code: > > def get_albums(self, parent_id = 0 ): > cursor = self.cnx.cursor() cursor = self.cnx.cursor(pymysql.cursors.DictCursor) > sql =( "select" > " id" > ", parent_id" > ", title" > ", ifnull( description, '' )" > ", path" > ", date( from_unixtime( date_created ) ) as date" > " from album" > " where parent_id = %(parent_id)s" > " order by date_created" > ) sql = ("""SELECT id, parent_id, ... """) > cursor.execute( sql, {'parent_id': parent_id } ) > rows = cursor.fetchall() now rows will looks like this: ({'id':...,...},{'id':...,}...) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Autotool - compile module for both Python 2 _and_ 3
Hi Dirk, On Tue, May 17, 2016 at 09:13:02AM +0200, Dirk Bächle wrote: > Hi Ervin, > > On 16.05.2016 11:05, Ervin Hegedüs wrote: > >Hi All, > > > > > >there is a library, which written in C. I'ld like to use it from > >Python - from Python 2 _and_ 3. > > > >I can make the autotools* files for Python 2 and Python 3, but > >only exclusively. I can't make it for both in same time. > > > >[...] > > > >Is there any "best practice" to solve this problem? > > > >Practical advice or links are welcome! > > > > here's my practical advice: have a look at SCons > (http://www.scons.org). It handles variant builds (multiple > packages/target) from the same source very well, even in parallel. many thanks - I'll check it out soon. Thanks, a. -- https://mail.python.org/mailman/listinfo/python-list
Autotool - compile module for both Python 2 _and_ 3
Hi All, there is a library, which written in C. I'ld like to use it from Python - from Python 2 _and_ 3. I can make the autotools* files for Python 2 and Python 3, but only exclusively. I can't make it for both in same time. There is a macro for autotool, called ax_python_devel.m4. This macro uses same names, so when I use it, and pass the arguments (eg. --with-python --with-python3), then only the latest name will be used (eg. PYTHON_CPPFLAGS = -I/usr/include/python3.4m), and compile system uses only that for both versions. Is there any "best practice" to solve this problem? Practical advice or links are welcome! Thanks, Ervin -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Testing whether the VPN is running?
Hi Adam, On Thu, Feb 18, 2016 at 09:26:58AM +, Adam Funk wrote: > I'd like to test (inside a python 3 program) whether the VPN is > running or not. The only thing I can think of so far is to use > subprocess to run the 'ifconfig' command, then check its output for > 'tun0'. Is there a better way? you didn't wrote, which system (OS) you want to use - based on the "ifconfig" and "tun0" keywords, possible that's a Linux. I think that the psutil modul could be better for you for this task: https://pypi.python.org/pypi/psutil/ and see the "Network" section. Regards, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
[issue25736] smtplib can't send 8bit encoded utf-8 message
Ervin Hegedüs added the comment: Here is a workaround: server.sendmail(mailfrom, rcptto, msg.encode("utf8")) May be this would be better inside of smtplib? -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25736> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25736] smtplib can't send 8bit encoded utf-8 message
Ervin Hegedüs added the comment: David, many thanks for your information. I think my e-mail format was correct - I've copied it from a maildir, as an "email file". As I wrote, there is a solution: before the code passes the 'msg' argument to sendmail() function, it needs to encode() it as "utf-8", then it will be a bytestream, instead of unicode (which is the default type of any string in Py3). Meanwhile I realized it :). Thanks again, and sorry for my mistake. -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25736> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25736] smtplib can't send 8bit encoded utf-8 message
Changes by Ervin Hegedüs <airw...@gmail.com>: -- nosy: airween priority: normal severity: normal status: open title: smtplib can't send 8bit encoded utf-8 message type: behavior versions: Python 3.4 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25736> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25736] smtplib can't send 8bit encoded utf-8 message
New submission from Ervin Hegedüs: Looks like smtplib can send only messages, which contains only 7bit (ascii) characters. Here is the example: # -*- coding: utf8 -*- import time import smtplib mailfrom = "m...@mydomain.com" rcptto = "m...@otherdomain.com" msg = """%s From: Me <%s> To: %s Subject: Plain text e-mail MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit happy New Year Ευτυχισμένο το Νέο Έτος 明けましておめでとうございます с Новым годом """ % (time.strftime('%a, %d %b %Y %H:%M:%S +0100', time.localtime()), mailfrom, rcptto) server = smtplib.SMTP('localhost') server.sendmail(mailfrom, rcptto, msg) server.quit() With Python2 (Python 2.7), this script finished succesfully. With Python3 (Python 3.4), I've got this execption: Traceback (most recent call last): File "8bittest.py", line 28, in server.sendmail(mailfrom, rcptto, msg) File "/usr/lib/python3.4/smtplib.py", line 765, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode characters in position 261-271: ordinal not in range(128) Basicly, I don't understand, why smtplib allows only ascii encoded messages in Python 3. That worked (and works) in Python 2, and I think, that's the correct behavior. -- ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25736> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
String format - resolve placeholders names
Hi, Python has a good string formatter, eg. I can do this: s = "{who} likes {what}" d = {'who': "Adam", 'what': "ants"} s.format(**d) result: 'Adam likes ants' Is it possible, and if yes, how to resolve the placeholders names in string? There is a know method: d1 = {'who1': "Adam", 'what1': "ants"} try: s.format(**d1) except KeyError: print("keyword missing") (gives 'keyword missing' as result). But is there any other (direct) way, which keywords exists in string? Thanks, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: String format - resolve placeholders names
Hello Chris, On Sat, Nov 21, 2015 at 02:06:11AM +1100, Chris Angelico wrote: > On Sat, Nov 21, 2015 at 1:52 AM, Ervin Hegedüs <airw...@gmail.com> wrote: > > Python has a good string formatter, eg. I can do this: > > > > s = "{who} likes {what}" > > d = {'who': "Adam", 'what': "ants"} > > s.format(**d) ... > > But is there any other (direct) way, which keywords exists in > > string? > > I think what you're asking for can be done using format_map with a > custom mapping object: > > >>> class IdentiMap: > ... def __init__(self): > ... self.keys = [] > ... def __getitem__(self, key): > ... self.keys.append(key) > ... > >>> m = IdentiMap() > >>> "{who} likes {what}".format_map(m) > 'None likes None' > >>> m.keys > ['who', 'what'] > > Does that help? absolutely, many thanks again! a. -- https://mail.python.org/mailman/listinfo/python-list
Re: String format - resolve placeholders names
Hi Denis, On Fri, Nov 20, 2015 at 05:08:21PM -, Denis McMahon wrote: > On Fri, 20 Nov 2015 16:53:47 +0100, Peter Otten wrote: > > > Ervin Hegedüs wrote: > > >> Python has a good string formatter, eg. I can do this: > > Or even: > > >>> s = "{who} likes {what}" > >>> d = {'who': "Adam", 'what': "ants"} > >>> keys = [x[1] for x in string.Formatter().parse(s)] > >>> keys > ['who', 'what'] > > then ... > > for key in keys: > if key not in d: > raise KeyError("Missing key '{}' in format string '{}'".format > (key, s)) well, I think I'm very satisfied - I got better and more better solutions :). Thanks for all, folks: a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: String format - resolve placeholders names
Hi Peter, On Fri, Nov 20, 2015 at 04:53:47PM +0100, Peter Otten wrote: > Ervin Hegedüs wrote: > > > Python has a good string formatter, eg. I can do this: > > > > s = "{who} likes {what}" > > d = {'who': "Adam", 'what': "ants"} > > s.format(**d) > > > > result: > > 'Adam likes ants' > > > > Is it possible, and if yes, how to resolve the placeholders names > > in string? > > >>> import string > >>> for item in string.Formatter().parse("{who} likes {what}"): > ... print(item) > ... > ('', 'who', '', None) > (' likes ', 'what', '', None) nice solution, thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
Hi, I've read many docs and examples, then I made a usable test version. If anybody interested about this (and for the mailing list archives), then it could be found here: https://code.activestate.com/recipes/579110-add-function-to-__builtin__-module-through-c-api/ Hope this helps, and many thanks for all help. Cheers, a. ps: after I've done, I realized, that will not good for me :). Nevermind, this was a funny work. On Tue, Oct 13, 2015 at 1:36 PM, Ervin Hegedüs <airw...@gmail.com> wrote: > Hello there, > > I'm interesting for the embeding of Python code - the examples and docs > are very helpfully. The main code, which embeds the Python interpreter, had > written in C. There are several functions, what I have to use in embedded > (Python) code, so I must to write them as Python extension. > > That's no problem - I found a solution for that, I don't need to made (and > I don't _want_) a separated .so file (a new Python module): the extension > in same C source, where the embedded code exists, like this: > > #include > #include > > /* python mymodule */ > static PyObject* > mymodule_usleep(PyObject *self, PyObject *args) > { > ... > } > > ... > static PyMethodDef mymodule_methods[] = { > {"usleep",mymodule_usleep,METH_VARARGS, > mymodule_usleep_doc}, > {NULL, NULL} > }; > > PyMODINIT_FUNC > initmymodule(void) > { > (void) Py_InitModule("mymodule", mymodule_methods); > } > > /* python mymodule */ > > > /* python embedding */ > void foo() { > Py_Initialize(); > initmymodule(); > > Py_Finalize(); > } > > /* python embedding */ > > > Then I have a Python file, which the code above calls: > > import mymodule > > def bar(d) > do_something() > mymodule.usleep(1) > return something > > > Just my "2 cents" question: is there any way to make the extension without > "import" keyword? Or is there a way to leave that? > > > Thanks, > > > a. > -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
On Wed, Oct 14, 2015 at 12:02:36AM +0200, Laura Creighton wrote: > In a message of Tue, 13 Oct 2015 22:28:54 +0200, Ervin Hegedüs writes: > >Hi Chris, > > > >what I misses: currently I'm using Python 2.7. > > > >On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote: [...] > > > >PyModule_AddFunction was introduced in Python 3.5. Most of stable > >Linux distribution has Python 3.4 > > > >> instead of the current module initialization. You import the name > >> 'builtins', stuff some extra stuff into it, and then go on your merry > >> way. It should be reasonably easy. > > > >Is there any other solution to add functions to builtins? > > > > You can stuff things into the __dict__ of __builtin__ if you like. > It's highly frowned upon. > But see discussion attatched to: > http://code.activestate.com/recipes/577888-see-what-the-builtins-are/ As I understand this, it shows the Python's builtins (or __builtins__) capabilities. As Chris wrote, the soultion would be, that I'm loading the __builtins__ module in C (through API), and add/extend its __dict__ with my funtions. The link above doesn't help me in this :). Thanks, a. -- https://mail.python.org/mailman/listinfo/python-list
Extended functions in embedded code
Hello there, I'm interesting for the embeding of Python code - the examples and docs are very helpfully. The main code, which embeds the Python interpreter, had written in C. There are several functions, what I have to use in embedded (Python) code, so I must to write them as Python extension. That's no problem - I found a solution for that, I don't need to made (and I don't _want_) a separated .so file (a new Python module): the extension in same C source, where the embedded code exists, like this: #include #include /* python mymodule */ static PyObject* mymodule_usleep(PyObject *self, PyObject *args) { ... } ... static PyMethodDef mymodule_methods[] = { {"usleep",mymodule_usleep,METH_VARARGS,mymodule_usleep_doc}, {NULL, NULL} }; PyMODINIT_FUNC initmymodule(void) { (void) Py_InitModule("mymodule", mymodule_methods); } /* python mymodule */ /* python embedding */ void foo() { Py_Initialize(); initmymodule(); Py_Finalize(); } /* python embedding */ Then I have a Python file, which the code above calls: import mymodule def bar(d) do_something() mymodule.usleep(1) return something Just my "2 cents" question: is there any way to make the extension without "import" keyword? Or is there a way to leave that? Thanks, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
Hi Chris, On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote: > On Wed, Oct 14, 2015 at 1:59 AM, Ervin Hegedüs <airw...@gmail.com> wrote: > > no, I have filesystem. I help to contribute a software, which had > > written in C. The configuration schema is very simple, there are > > several keywords, but not all required function could be > > configure with them. Python would be a good choice, but the > > users aren't programmers in most cases. I just don't want to > > start the documentation, that > > > > "Make a Python script like this: > > > > import mymodul > > > > " > > > > and nobody knows, why is it require, because there isn't any > > module with this name. > > Sounds to me like the easiest way would be to inject into the > builtins. You should be able to import the builtins module from your C > code, and then stuff some extra attributes into it; they'll be > automatically available to the script, same as the "normal" built-in > names like int, super, and ValueError. well, sounds good - this solution would be right for me. Could you show me a good example and/or documentation about this? I've looked up, but "python extend built-in module" is may be too simple expression :). a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
Hi, On Tue, Oct 13, 2015 at 02:51:21PM +0200, Laura Creighton wrote: > Are you looking for this:? > https://docs.python.org/3.5/library/runpy.html I think I'm not - I'm afraid, the runpy modul wasn't developed for me, for this reason. > or maybe this:? > https://docs.python.org/3.5/library/importlib.html#importlib.import_module I think this is just an "alias" of the standard import. > Or is your real problem 'I don't have a filesystem'? no, I have filesystem. I help to contribute a software, which had written in C. The configuration schema is very simple, there are several keywords, but not all required function could be configure with them. Python would be a good choice, but the users aren't programmers in most cases. I just don't want to start the documentation, that "Make a Python script like this: import mymodul " and nobody knows, why is it require, because there isn't any module with this name. As I wrote, that's just my 2-cents question, not a critical issue - it could be better to know, is it possible or not. That's all :) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
Hi Chris, what I misses: currently I'm using Python 2.7. On Wed, Oct 14, 2015 at 02:48:57AM +1100, Chris Angelico wrote: > On Wed, Oct 14, 2015 at 2:29 AM, Ervin Hegedüs <airw...@gmail.com> wrote: > >> > >> Sounds to me like the easiest way would be to inject into the > >> builtins. You should be able to import the builtins module from your C > >> code, and then stuff some extra attributes into it; they'll be > >> automatically available to the script, same as the "normal" built-in > >> names like int, super, and ValueError. > > > > well, sounds good - this solution would be right for me. Could > > you show me a good example and/or documentation about this? I've > > looked up, but "python extend built-in module" is may be too > > simple expression :). > > It'd look broadly like this: > > /* initialize the interpreter, yada yada */ > PyObject *builtins = PyImport_ImportModule("builtins"); > PyModule_AddFunctions(builtins, mymodule_methods); PyModule_AddFunction was introduced in Python 3.5. Most of stable Linux distribution has Python 3.4 > instead of the current module initialization. You import the name > 'builtins', stuff some extra stuff into it, and then go on your merry > way. It should be reasonably easy. Is there any other solution to add functions to builtins? Thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Extended functions in embedded code
Hi, On Tue, Oct 13, 2015 at 08:55:42AM -0700, Emile van Sebille wrote: > On 10/13/2015 8:29 AM, Ervin Hegedüs wrote: > >Hi Chris, > > > >On Wed, Oct 14, 2015 at 02:05:43AM +1100, Chris Angelico wrote: > > >>Sounds to me like the easiest way would be to inject into the > >>builtins. You should be able to import the builtins module from your C > >>code, and then stuff some extra attributes into it; they'll be > >>automatically available to the script, same as the "normal" built-in > >>names like int, super, and ValueError. > > > >well, sounds good - this solution would be right for me. Could > >you show me a good example and/or documentation about this? I've > >looked up, but "python extend built-in module" is may be too > >simple expression :). > > Maybe the site module helps you. See > https://docs.python.org/3/library/site.html no, I think this module is totally different, what I need. thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: function code snippet that has function calls I have never seen before. How does it work.
hi, On Sat, Oct 03, 2015 at 10:40:57AM -0700, Ronald Cosentino wrote: > def funA(x,y,z): > return (x+y) * z > def funB(x,y): > return(x-y) > print(funA(4,funB(2,3), funB(3,2))) > > the answer is 3. I don't know how it works. it's simple: - there is a "composition of functions", generally f(g()) (function in argument list of another function) - first, Python evaulates the arguments first, from left to right - in this point, you'll get -1 for 2nd arg, and 1 for 3rd arg - then your funcA() will be called with these arguents: 4, -1, 1 - funcA() calculates this: (x+y)*z, in this case (4+(-1))*1 which is 3... a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Python xlrd
hi, On Sat, May 02, 2015 at 03:12:46PM -0700, lbertolotti via Python-list wrote: Ubuntu terminal gives me: import xlrd I get ImportError: No module named xlrd Any ideas? I installed it by: sudo apt-get install python-xlrd what's the answers of your system for these questions? dpkg -C // check the broken packages dpkg -l python-xlrd // check the status of xlrd package a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Lockfile hanling
Hello, there is an app, written in Python, which stores few bytes of datas in a single file. The application uses threads. Every thread can modify the file, but only one at a time. I'm using a lock file to prevent the multiple access. Here is the lock method: while True: try: fl = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR) except OSError, e: if e.errno != errno.EEXIST: raise time.sleep(0.2) continue except: syslog.syslog(syslog.LOG_DEBUG, Sync error: + str(sys.exc_info()[1])) else: break This works as well for me - about 3-4 weeks. After some weeks, I got this error: OSError: [Errno 24] Too many open files: '/var/spool/myapp/queue.lock' Today the app had been restarted, about 3-4 hours ago. Now I see under the proc/PID/fd: lrwx-- 1 root root 64 márc 31 16.45 5 - /var/spool/myapp/queue.lock (deleted) there are about 50 deleted FD's. After few weeks the process reaches the number if max fd's. How can I prevent or avoid this issue? What's the correct way to handle the lockfile in Python? Thanks, Ervin -- https://mail.python.org/mailman/listinfo/python-list
Re: Lockfile hanling
Hi Skip, thanks for the reply, On Tue, Mar 31, 2015 at 09:55:57AM -0500, Skip Montanaro wrote: On Tue, Mar 31, 2015 at 9:50 AM, Ervin Hegedüs airw...@gmail.com wrote: After few weeks the process reaches the number if max fd's. How can I prevent or avoid this issue? What's the correct way to handle the lockfile in Python? Ervin, You need to close the files you open. I don't see that happening in your code snippet. sorry - at the end of the function there is a close() method to a file, after the thread passes the modifications: try: os.remove(self.lockfile) except: syslog.syslog(syslog.LOG_DEBUG, Sync error: + str(sys.exc_info()[1])) And I think, the (deleted) info near the filename in proc/PID/fd means the lockfile had been deleted. You might consider the pylockfile module, available from PyPI: https://pypi.python.org/pypi/lockfile (I'm the original author, though others have taken it over.) sounds good, thanks - what's the minimal version of Python? I've only 2.5 on that server... Regards, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Lockfile hanling
Hi Skip, On Tue, Mar 31, 2015 at 10:19:27AM -0500, Skip Montanaro wrote: sorry - at the end of the function there is a close() method to a file, after the thread passes the modifications: try: os.remove(self.lockfile) except: syslog.syslog(syslog.LOG_DEBUG, Sync error: + str(sys.exc_info()[1])) Hmmm... Still not seeing os.close(fl)... And I think, the (deleted) info near the filename in proc/PID/fd means the lockfile had been deleted. os.remove() will remove the lockfile name from the directory. It doesn't automatically close the file. ah, well, many-many-many thanks - that was what I misses... You might consider the pylockfile module... sounds good, thanks - what's the minimal version of Python? I've only 2.5 on that server... When I was still maintaining it, it worked with 2.5, and I distributed a patch you could apply to get it to work with 2.4. I'm not sure if the current maintainers have changed that. You'd have to look. If worse comes to worst, I believe 0.9.1 (the last version I released) should still work with 2.5. thanks for the info - now it works as very well. a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Lockfile hanling
Hi Matthew, On Tue, Mar 31, 2015 at 11:50:06AM -0400, Matthew Ruffalo wrote: On 2015-03-31 10:50, Ervin Hegedüs wrote: there is an app, written in Python, which stores few bytes of datas in a single file. The application uses threads. Every thread can modify the file, but only one at a time. I'm using a lock file to prevent the multiple access. ... How can I prevent or avoid this issue? What's the correct way to handle the lockfile in Python? Hi Ervin- If only one instance of the app is running at a given time, and you only need to ensure mutual exclusion between its threads, you should probably not use a lock *file* for this. I would strongly recommend that you use a threading.Lock as per https://docs.python.org/2.5/lib/module-threading.html instead of a lock file. This will also allow you to avoid a 0.2-second polling loop; a call to threading.Lock.acquire() will block until it is released by another thread. MMR... thanks, I hope in the next version I can try this :) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I remove/unlink wildcarded files
Hi, On Thu, Jan 01, 2015 at 05:13:31PM -0600, Anthony Papillion wrote: Hi Everyone, I have a function I'm writing to delete wildcarded files in a directory. I tried this: def unlinkFiles(): os.remove(/home/anthony/backup/unix*) This doesn't seem to work because it's a wildcard filename. What is the proper way to delete files using wildcards? Now I didn't checked, but once I've used some like this: def unlinkFiles(): dirname = /path/to/dir for f in os.listdir(dirname): if re.match(^unix*$, f): os.remove(os.path.join(dirname, f)) a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I remove/unlink wildcarded files
Hi, On Fri, Jan 02, 2015 at 09:21:53PM +1100, Cameron Simpson wrote: On 02Jan2015 10:00, Ervin Hegedüs airw...@gmail.com wrote: On Thu, Jan 01, 2015 at 05:13:31PM -0600, Anthony Papillion wrote: I have a function I'm writing to delete wildcarded files in a directory. I tried this: def unlinkFiles(): os.remove(/home/anthony/backup/unix*) This doesn't seem to work because it's a wildcard filename. What is the proper way to delete files using wildcards? Now I didn't checked, but once I've used some like this: def unlinkFiles(): dirname = /path/to/dir for f in os.listdir(dirname): if re.match(^unix*$, f): os.remove(os.path.join(dirname, f)) That is a very expensive way to check the filename in this particular case. Consider: if f.startswith('unix'): instead of using a regular expression. well, that's true - but that works only the example above. But generally the OP will probably want to use the glob module to expand the shell pattern as suggested by others. I didn't know the glob module before, but yes, that's better solution for this - but as I see, that also uses os.listdir() (and fnmatch modue). Anyway, thanks for the tip. a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I remove/unlink wildcarded files
Hi, On Fri, Jan 02, 2015 at 11:59:17PM +1100, Chris Angelico wrote: On Fri, Jan 2, 2015 at 11:36 PM, Ervin Hegedüs airw...@gmail.com wrote: And worse, the given re would delete a file named uni which doesn't sound ANYTHING like what the OP wanted :-) yes, you're right - I've missed out a . before the *. :) Another reason to avoid regexps when you don't actually need them. Globs are simpler, and have fewer obscure failure modes. it may be at the concrete example in OP is better the glob - but I think in most cases the re modul gives more flexibility, I mean the glob modul can handle the upper/lower chars? (Anyway, now I checked the fnmatch module, which uses re module :)) Cheers: a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I remove/unlink wildcarded files
Hi, On Fri, Jan 02, 2015 at 05:35:52AM -0600, Tim Chase wrote: On 2015-01-02 21:21, Cameron Simpson wrote: def unlinkFiles(): dirname = /path/to/dir for f in os.listdir(dirname): if re.match(^unix*$, f): os.remove(os.path.join(dirname, f)) That is a very expensive way to check the filename in this particular case. Consider: if f.startswith('unix'): instead of using a regular expression. And worse, the given re would delete a file named uni which doesn't sound ANYTHING like what the OP wanted :-) yes, you're right - I've missed out a . before the *. :) thanks: a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I remove/unlink wildcarded files
Hi Chris, On Sat, Jan 03, 2015 at 01:01:31AM +1100, Chris Angelico wrote: On Sat, Jan 3, 2015 at 12:51 AM, Ervin Hegedüs airw...@gmail.com wrote: it may be at the concrete example in OP is better the glob - but I think in most cases the re modul gives more flexibility, I mean the glob modul can handle the upper/lower chars? I'm not sure that I'd want to. Handling case insensitivity is fine when you're restricting everything to ASCII, but it's rather harder when you allow all of Unicode. For instance, U+0069 and U+0049 would be considered case-insensitively equal in English, but in Turkish, they're different letters; U+0069 upper-cases to U+0130, and U+0049 lower-cases to U+0131. Much safer, when you're writing generic tools, to simply require case sensitivity. And you can't easily know whether the file system is case-sensitive, case-retaining, or case-folding; you could easily have multiple mounts that differ, so /home/rosuav/foo/bar/quux might be the same as /home/rosuav/FOO/bar/quux, but the other four components are all case sensitive. Yes, it really is possible. I didn't want to solve the OP's problem - I just gave an idea. Here was another possible solution, I think the OP can choose the right one :) Cheers: a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: smptplib problem SMTPRecipientsRefused for emails with ! exclamation mark in local portion of email
Hi, On Wed, Dec 17, 2014 at 08:16:23AM -0800, radzh...@gmail.com wrote: smtplib.SMTPRecipientsRefused: {'aahlin!@gmail.com': (550, 'restricted characters in address')} As in this question, the answer has reference to RFCs that spec it out, and state that exclamations are ok, so why is smptplib throwint this error? http://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-email-address Django uses smtplib to send emails so it in turn fails too but email validation allows you to enter ! I think that this exception came from your SMTP server, not from smtplib. Check your smtpd log. You can try to send through that smtpd at another way (eg. netcat) with that address - may be then you can check the result. a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread-ID - how much could be?
On Sat, Sep 13, 2014 at 12:09:28PM +0200, Martin Skjöldebrand wrote: Unfortunately we will never know hehe :), joke of the day :) thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Example of python service running under systemd?
Hi Michael, On Thu, Sep 11, 2014 at 08:03:54PM -0600, Michael Torrie wrote: What I want is to have this startup, after my board has it’s networking layer up and running (and hopefully a valid ip address by then), and to just keep running forever may be you think about the fork(), eg: No, you you don't need to do this. Systemd can handle all of that for you. Read up on the docs on creating systemd services. Here's a little blog post that has some good examples, both a non-daemonizing service and a daemonizing service: http://patrakov.blogspot.com/2011/01/writing-systemd-service-files.html Any executable file can be turned into a daemon service with systemd (whether or not it forks itself into the background). Thus any python script can easily be run from systemd. thanks a lot, I didn't hear about that feature. Cheers, Ervin -- https://mail.python.org/mailman/listinfo/python-list
Re: Example of python service running under systemd?
Hi Chris, On Fri, Sep 12, 2014 at 12:29:27PM +1000, Chris Angelico wrote: On Fri, Sep 12, 2014 at 12:03 PM, Michael Torrie torr...@gmail.com wrote: Any executable file can be turned into a daemon service with systemd (whether or not it forks itself into the background). Thus any python script can easily be run from systemd. I strongly recommend making a non-daemonizing service. It's so much easier to debug - there's one mode of operation, the script just runs. You can then run that directly in a terminal, or via tmux, or via systemd - and I've done all three with Yosemite. In fact, I think I have instances here on the LAN that are doing all three, right now! is there any other reason outside the debugging? Of course, I've handled that in a simple way: parser = optparse.OptionParser() parser.add_option(-d, --debug, action=count, dest=debug_mode, help=Start process in debug mode, not forking.) (options, args) = parser.parse_args() debug_mode = True if options.debug_mode is None: debug_mode = False try: pid = os.fork() if pid 0: And of course, I've handled the signals, logfiles and so on... So, now I can run my app with -d, then it will not do the fork(), I'll see all messages and feedbacks. Elsewhere, the process will run in background. Anyway, thanks all comments from others. May be the life is easier with systemd, but that was my 5-minutes-finger-exercise :) Thanks again, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread-ID - how much could be?
Hi Steven, On Fri, Sep 12, 2014 at 11:29:56AM +1000, Steven D'Aprano wrote: import sys print sys.maxint 9223372036854775807 the couter could be 9223372036854775807? And after? :) Suppose you somehow managed to create 9223372036854775807 threads. If your computer has 16 GB of RAM available, that means that at most each thread can use: so, thanks for your and others answers - this was just a _theoretical_ question. What is the practice - that's an another thread. :) I just simply interested about this theory, not more. I don't care with-how-many-memories-needs-and-how-many-years-to-overflow that counter, but many people calculates that - thanks :) Cheers, a. -- https://mail.python.org/mailman/listinfo/python-list
Thread-ID - how much could be?
Hello, I've made a long-time running daemon, which uses threads. Looks like that works perfectly, now I'm looking at the exceptions :). In the log, I found an interesting message: Exception in thread Thread-82: ... The main function allows 2 thread to run simultaniously, and if the thread finished, then it joined with th.join(), where the th is the thread item, derived from threading.Thread class. My question is: how much thread ID could be totally? Is there any maximum number? And if the thread reached that, what will be done? Overlflowed? Couting from 0 again? Thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Example of python service running under systemd?
Hi Travis, On Thu, Sep 11, 2014 at 02:06:48PM -0700, Travis Griggs wrote: On Sep 11, 2014, at 11:18 AM, Chris “Kwpolska” Warrick kwpol...@gmail.com wrote: Depends what you want. Mine is not a web service. My main.py looks like this: #!/usr/bin/env python3 import cycle import pushTelemetry from threading import Thread def main(): Thread(target=pushTelemetry.udpLoop).start() Thread(target=cycle.cycleLoop).start() if __name__ == '__main__': main() It basically creates two threads, one which does some local processing and control, the other which periodically does reporting via udp packets. I use the dual threads because they both work with a shared serial port at times, so I have to synchronize access through that. What I want is to have this startup, after my board has it’s networking layer up and running (and hopefully a valid ip address by then), and to just keep running forever may be you think about the fork(), eg: if __name__ == __main__: ...other codes, eg. drop root privileges, ... ...check arguments... try: pid = os.fork() if pid 0: #print Daemon started (pid: %d) % (pid) sys.exit(0) except OSError, e: print sys.stderr, fork #1 failed: %d (%s) % (e.errno, e.strerror) sys.exit(1) os.chdir(/) os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid 0: #print Daemon started (pid: %d) % (pid) sys.exit(0) except OSError, e: print sys.stderr, fork #2 failed: %d (%s) % (e.errno, e.strerror) sys.exit(1) main() regards, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread-ID - how much could be?
Hi Peter, thanks for the reply, On Thu, Sep 11, 2014 at 09:48:18PM +0200, Peter Otten wrote: Ervin Hegedüs wrote: Exception in thread Thread-82: ... My question is: how much thread ID could be totally? Is there any maximum number? And if the thread reached that, what will be done? Overlflowed? Couting from 0 again? A quick peak into threading.py reveals # Helper to generate new thread names _counter = 0 def _newname(template=Thread-%d): global _counter _counter += 1 return template % _counter class Thread: ... def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None): ... self._name = str(name or _newname()) There is no upper limit to the thread name other than that you will eventually run out of memory ;) thanks - I hope that the memory will not run out by these threads... :) Anyway, that means, on my system: import sys print sys.maxint 9223372036854775807 the couter could be 9223372036854775807? And after? :) Thanks, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread terminate
Hi Chris, thanks for the reply, On Wed, Aug 27, 2014 at 12:26:48PM -0700, Chris Kaynor wrote: On Wed, Aug 27, 2014 at 11:55 AM, Ervin Hegedüs airw...@gmail.com wrote: what's the correct way to terminate a thread by itself? To terminate the thread, the run function must exit. This can be either from an exception or a return statement. you mean: def run(self): do_domething() return True is enough? I mean: class MyThread(threading.Thread): def __init__(self, queueitem): threading.Thread.__init__(self) ... def run(self): pseudo code below try: self.connect_to_database() except: First off, especially if you are going to raise an exception anyways, I would recommend not silencing the orignal error - it will make debugging harder. That's right - I just copied the relevant code. This is especially true with a bare except: statement - at least capture only some exceptions. Using a logger to handle the exception or use your except, log the error, than reraise with a bare raise statement would generally be preferable. self.terminate(Error when connect to db) thanks for the idea, try: self.create_db_table() except: self.terminate(Error when create table) You probably want to make sure to disconnect from the database in here. Possibly via a with statement or a try...finally over the entire block, depending on your database API. right, in main(): for q in range(0, 10): mt = MyThread() try: mt.run() This is not actually starting a thread, but is running it in-line, just like a normal function. You probably want mt.start() instead, which will cause mt.run to execute in another thread. yes, I know that, and I've used mt.start() before the mt.run() - I just missed out to paste that part of code - sorry. If you use mt.start() to actually start a thread, the only time it will fail is if the thread fails to start (such as too many running threads, or other out-of-resource errors). The exceptions raised inside the thread will not propagate outside the start call, and thus the mt.join() call inside the except statement will have no effect - no thread is running. yes, I thought that, this triggered my question :) but it doesn't works - the exception showed on stdout only, and the thread stops of course, but I would like to handle the exceptions. If I call the join() inside from thread object, then an exception raised, cause the thread object of Threading module doesn't allows that... Not being able to call join from the thread you are joining with is probably a good thing - doing so would result in a dead lock: the thread is waiting for the thread to exit. ah, well, I understand it now... In your case, you may want to just handle the exceptions inside the thread's run() function directly. If that is not possible and you really need to handle them inside the main thread, you would need to store off the error data in a variable (either passed into the thread as the args or kwargs arguments to the MyThread() call, or global or class variables) then use mt.join() to wait for the thread(s) to exit. no, I don't need to handle it outside of thread - the point is when the exception raised in a running thread, then the exception be logged (that's no problem), and thread be stopped state, so the caller function is able to call the join() that thread. In the case of handling the problems in the main thread, your main thread code would look something like: # Start all of the threads up-front. threads = [] for q in range(0, 10): mt = MyThread() mt.start() # NOT mt.run() - that does not do any threading! threads.append(mt) # Now wait for them to complete. for thread in threads: thread.join() # Check for the status of thread here. Possibly on the thread object, which can be mutated in MyThread.run by assigning to members of self. yes, the pseudo structure of my code is same as your here. But the now the problem is when an exception raised, that showed at stdout too, not just the log. The process will be forked, and will runs in background when I'll finished that, so it could be good to controll all messages. In the sample code you are doing, I am not even sure if I would use threads due to the added complexities associated with them. In the case of merely connecting to a database and creating a few tables, you can probably get by with doing them in a single thread: open a single connection, creating the tables as a sequence of operations, commit operations, and close connection. at this time there is only one thread, as you wrote. I just try to prepare it to higher load, when one thread will not enough... If you do in fact need threads, you may also be better off using a thread pool (see concurrent.futures
Re: Thread terminate
Hi Marko, On Thu, Aug 28, 2014 at 12:02:29PM +0300, Marko Rauhamaa wrote: Ervin Hegedüs airw...@gmail.com: at this time there is only one thread, as you wrote. I just try to prepare it to higher load, when one thread will not enough... Threads are a necessary evil when dealing with blocking function calls, but evil they remain. I would generally *not* associate a thread for each parallel context but work with a limited thread pool, whose size is independent of the number of contexts. Often, a good rule of thumb is to have two threads per CPU core to get most out of the hardware. thanks - I didn't plan to increase of size of thread pool to greather than 2-4 - the server has 4 CPU. Since you talked about forking, you might be better served with a pool of processes instead of threads since processes have less surprising semantics and, if push came to shove, you can kill them with a signal. life with threads are better in my case - the shared memory is a big advantage. That's the only reason... :) Thanks, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread terminate
Hi Chris, thanks for you answer, On Thu, Aug 28, 2014 at 09:23:24AM -0700, Chris Kaynor wrote: On Thu, Aug 28, 2014 at 1:52 AM, Ervin Hegedüs airw...@gmail.com wrote: In your case, you may want to just handle the exceptions inside the thread's run() function directly. If that is not possible and you really need to handle them inside the main thread, you would need to store off the error data in a variable (either passed into the thread as the args or kwargs arguments to the MyThread() call, or global or class variables) then use mt.join() to wait for the thread(s) to exit. no, I don't need to handle it outside of thread - the point is when the exception raised in a running thread, then the exception be logged (that's no problem), and thread be stopped state, so the caller function is able to call the join() that thread. In this case, all that needs to happen is for the thread's run function to either throw an exception (as happens in the error case in your sample code) or return. now the code looks like this: def run(self): try: connect_to_db() except: self._exit(-1, Connection error, sys.exc_info()[1]) return True try: do_another_thing() except: self._exit(-2, Another error, sys.exc_info()[1]) return True so, the thread's _exit() functions set many things, logs to exception error, but not terminate the thread. As you can see, all except line of try block contains a return - that's what I wanted to avoid. The threading module will cause it to print any exception that occurs by default. yes, I saw that :), yes, the pseudo structure of my code is same as your here. But the now the problem is when an exception raised, that showed at stdout too, not just the log. If what you want is to make sure the error is not printed to stderr, you'll just need to make sure the thread's run function does not exit with an exception. The simpliest way to do that would be to wrap the entire thread's run function in a try...catch statement, like so: I've re-indented your code below: class Thread(threading.Thread) def run(self): try: # Do your code here. # You can also have more specific error-handling inside here if needed. except Exception as err: # Log your error here - you will be silencing it and therefore unable to # see it in the TTY! Now I don't see the key different between your code, and my example - looks like there are same. But when I tried to throw an exception, that showed in TTY. Nevermind, now it works, only the necessary return keyword would be good to leave. # If you want to be able to handle the errors in the main thread, you could run code like the following: #self.err = err # Where you can check and access them on the main thread. return # Optional if there is no other code outside the try...except. I don't see your opinion from this snippet, but I think my solution is very similar like yours :) Thanks for the help again, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Thread terminate
Hi Chris, On Thu, Aug 28, 2014 at 12:34:18PM -0700, Chris Kaynor wrote: On Thu, Aug 28, 2014 at 11:39 AM, Ervin Hegedüs airw...@gmail.com wrote: now the code looks like this: def run(self): try: connect_to_db() except: self._exit(-1, Connection error, sys.exc_info()[1]) return True try: do_another_thing() except: self._exit(-2, Another error, sys.exc_info()[1]) return True You could also do this this way: def run(self): try: try: connect_to_db() except: self._exit(-1, Connection error, sys.exc_info()[1]) raise # could do something similar inside self._exit instead. try: do_another_thing() except: self._exit(-2, Another error, sys.exc_info()[1]) raise # could do something similar inside self._exit instead. except: return True I'm not sure that is any clearer. thank's, may be I'll check this way soon, depending on what you are doing with the first two arguments to self._exit, the following might also work: def run(self): try: connect_to_db() do_another_thing() except: self._exit(*sys.exc_info()) return True The first argument is a status, this is passed to the item, which passed to thread - so the thread sets that status, and the main loop knows, which item needs to pass to a thread. Eg. if the DB connection has failed, it needs to run again, at later. But if that item is parsed and finished (eg. table exists in db), then the item is deletable from the queue. Of course, this has the issue that you do not get your extra information, however, from my experience, typically the error type, message, and traceback are sufficient, and rarely have I needed to provide additional details. right, that's clear. Now I don't see the key different between your code, and my example - looks like there are same. But when I tried to throw an exception, that showed in TTY. Nevermind, now it works, only the necessary return keyword would be good to leave. Effectively, your formatting is the same as what I was suggesting - I was merely adding that you could wrap the entire block inside of a single try..except rather than having multiple ones. That does have some drawbacks (as mentioned above). ok, One solution I did not mention before, but is plausible, is to monkey-patch/override the threading module to change the behavior of _bootstrap_inner or _bootstrap to behave differently. As these are on the Thread class, you could override them in your subclass or a base subclass (depending on the number of Thread subclasses you have) that behaves differently. Perhaps by making it call sys.excepthook. At one point, we discussed doing something similar at work as we normally print exceptions to custom streams that provide color formatting to make the exceptions easier to find and read (exception message printed in red, traceback in a dark red), as well as log. oh, that would be a big gun - and I don't feel that knowledge to made that with security and stability. I think this solution will enough at first time :), I didn't used anytime the threads (in serious project). Thanks for the help, a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Thread terminate
Hi, what's the correct way to terminate a thread by itself? I mean: class MyThread(threading.Thread): def __init__(self, queueitem): threading.Thread.__init__(self) ... def run(self): pseudo code below try: self.connect_to_database() except: self.terminate(Error when connect to db) try: self.create_db_table() except: self.terminate(Error when create table) def terminate(self, msg): self.error = True self.errormsg = msg syslog.syslog(syslog.LOG_DEBUG, msg) raise Exception(msg) in main(): for q in range(0, 10): mt = MyThread() try: mt.run() except: mt.join() but it doesn't works - the exception showed on stdout only, and the thread stops of course, but I would like to handle the exceptions. If I call the join() inside from thread object, then an exception raised, cause the thread object of Threading module doesn't allows that... Any helps are welcome, greetings: a. -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Problems compiling Python 3.4 on Ubuntu
Hello, On Mon, Feb 03, 2014 at 02:50:15AM -0800, cool-RR wrote: Hi, I'm trying to install Python 3.4b3 on Ubuntu. Since compilation seems to be the only way, I'm trying that. I downloaded the source, I changed Setup.dist to have this: SSL=/usr _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto [...] ./Modules/_ssl.c:57:25: fatal error: openssl/rsa.h: No such file or directory #include openssl/rsa.h ^ compilation terminated. What do I do to solve this? try this: sudo apt-get install libssl-dev cheers, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with my 8-year old son's first program. I'm stuck!
Hello, On Sat, Jan 25, 2014 at 02:02:15AM -0800, justinpmull...@gmail.com wrote: My son is learning Python and I know nothing about computers. :) He's written a simple calculator program that doesn't work. For the life of me, I can't see why. Any help gratefully received. Here's his code: def a(): import sys print(welcome to the calculation) print(please type a number) one = int(sys.stdin.readline()) print(type d for division,) print(type m for multiplication,) print(type s for subtraction,) print(and type p for plus) op = (sys.stdin.readline()) print(%s selected % op) print(please enter another number) two = int(sys.stdin.readline()) if op == str(d): out == one / two print(the answer is %s % out) elif op == m: out == one * two print(the answer is %s % out) elif op == s: out == one - two print(the answer is %s % out) elif op == p: out == one + two print(the answer is %s % out) else: print(huh) Where is he going wrong? what's your error message? First, you have to put an interpreter in first line - if you're on Linux/Unix: #!/usr/bin/python or some kind of that. I don't know what's the expected on Windows :(. Second, you defined a funcfion, called a, and if you want to see how does it work, you must call that (after you define it): def a(): ... ... a() Third, at the first statement (if op == str(d)) you're referencing for an undefined variable (d), I think you would put `if op == d:', instad of that. Good luck, Ervin -- I � UTF-8 -- https://mail.python.org/mailman/listinfo/python-list
Re: Bytes indexing returns an int
hi, On Tue, Jan 07, 2014 at 10:13:29PM +1100, Steven D'Aprano wrote: Does anyone know what the rationale behind making byte-string indexing return an int rather than a byte-string of length one? That is, given b = b'xyz', b[1] returns 121 rather than b'y'. This is especially surprising when one considers that it's easy to extract the ordinal value of a byte: ord(b'y') = 121 Which Python version? http://docs.python.org/2/reference/lexical_analysis.html#strings A prefix of 'b' or 'B' is ignored in Python 2; if you want to store the string literal as byte array, you have to use bytearray() function: a = bytearray('xyz') a bytearray(b'xyz') a[0] 120 a[1] 121 http://docs.python.org/2/library/stdtypes.html 5.6. Sequence Types hth, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] trying socket as a replacement for nc
hello, #!/usr/bin/env python A simple echo client import socket as socket_mod import bufsock as bufsock_mod [...] Traceback (most recent call last): File ./buftest.py, line 11, in module socket = socket_mod.socket(socket.AF_INET, socket.SOCK_STREAM) NameError: name 'socket' is not defined you should replace the socket.AF_INET to socket_mod.AF_INET, and socket.SOCK_STREAM to socket_mod.SOCK_STREAM, if you've imported socket modul as socket_mod. But this is just an idea... :) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, mySQL and password
Hello, On Mon, Dec 16, 2013 at 02:55:29AM -0800, Igor Korot wrote: Hi, ALL, Is there a way to make python script that connects to mySQL DB ask for a password on the: conn = mdb.connect(host, user) line. The host variable is localhost and the user variable is root (for developmental purposes). may be you think some like this: import MySQLdb dsn = { 'host': 127.0.0.1, 'user': root, 'passwd': , 'port': 3306, 'db': test } dsn['passwd'] = raw_input(Enter password for %s: % (dsn['user'])) mysql = MySQLdb.connect(**dsn) cursor = mysql.cursor(MySQLdb.cursors.DictCursor) but at this way the password what you type will showing! a. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, mySQL and password
Hello Peter, On Mon, Dec 16, 2013 at 12:38:33PM +0100, Peter Otten wrote: Ervin Hegedüs wrote: dsn['passwd'] = raw_input(Enter password for %s: % (dsn['user'])) [...] but at this way the password what you type will showing! To avoid that use getpass.getpass() instead of raw_input(). http://docs.python.org/2/library/getpass.html well, thanks a lot :) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: TypeError: not all arguments converted during string formatting
hello, On Fri, Dec 13, 2013 at 05:18:59AM -0800, Jai wrote: sql = insert into fashion(GENDER,links,category,item_content,price,seller) \ VAlUES('%s','%s','%s','%s','%s','s') may be you miss a % sign? query = query % db.literal(args) TypeError: not all arguments converted during string formatting cheers: a. -- https://mail.python.org/mailman/listinfo/python-list
Re: urllib2 timeout issue
Hello, On Thu, Oct 17, 2013 at 03:34:05PM +0200, Jérôme wrote: Hi. Thank you all for your answers. -- Context: The problem I want to address is the code being stuck too long when the network is down. I'm working on a software gateway running on a Raspberry Pi, that forwards data received through a radio link to the network. https://github.com/Jerome-github/oem_gateway This can be sending HTTP requests every 3 seconds, so a 10 secondes timeout is an issue. -- I was not at home when I wrote my last message. Now back home, I could try on my own Debian Jessie machine (Python 2.7.5+) and I get the same results as with the RaspberryPi (long timeout). So there seems to be something going on with the router/modem. Has your router/modem any HTTP proxy feature? If yes, maybe the proxy gives the late answer. Anyway, if you don't have proxy, and the DNS error cames from local system, you can try to decrease it via resolv.conf: nameserver 1.2.3.4 options timeout:2 It's just an idea... but maybe... Note that the 2 seconds for DNS timeout in resolv.conf may sound crazy, because the _real_slow_ DNS answers will be timeouted for any application - just take a test with it. Cheers, Ervin -- https://mail.python.org/mailman/listinfo/python-list
Re: DNS query against a specific server.
Hello, On Mon, Sep 30, 2013 at 04:42:29AM -0700, Michel Albert wrote: Hi, ``socket.gethostbyname`` sends the DNS resolution query to the DNS server specified by the OS. Is there an easy way to send a query to a *different* server? I see that twisted.names allows you to do this, but, having all of twisted as dependency to my project when all I need to do is a simple DNS query seems a bit extreme. I also found pydns, but that looks fairly outdated and unmaintained. Is there not an actively maintained lightweight solution? If not, I will go with twisted. there is a dns modul for Python (I don't know is it part of standard Python library or not), on most Linux distribution you can find it, eg. in Debian it's called python-dnspython. It can handle different nameserver, than OS knows - here is a sample code: import dns.resolver r = dns.resolver.Resolver() r.namerservers = ['127.0.0.1'] # or any other IP, in my case I'm using PDNS, which have two # parts: a recursor and a resolver; recursor allows requests only # on localhost mxservers = r.query(python.org, 'MX').response hth, a. -- https://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb Help
Hello, On Tue, Sep 17, 2013 at 12:43:20PM +0530, Venkat Addala wrote: Hi all, I'm new to python, i am connecting mysql database with python. now i want to do sanitation on the database, like to remove \n, extra spaces blah blah.. and update it back to mysql database. i was trying somthing, here is my code, can you please provide me solution for this.. #!/usr/bin/python import MySQLdb as mdb con = mdb.connect('localhost', 'root', 'pass@123', 'workbench'); a tip: you can pass the arguments to function as dictionary, so if you store the connection arguments anywhere, you use this form: conndsc = {'host': 'localhost', 'user': 'root', 'port': 3306, } con = mdb.connect(**conndsc) My personal opinion, but it's easyer to use the cursor if you have dictionary-like cursor: with con: cur = con.cursor() cur = con.cursor(mdb.cursors.DictCursor) In this case you can reference the name of the table column, instead of index of result, in your example: row['descrip'] cur.execute(SELECT descrip FROM table LIMIT 1) rows = cur.fetchall() if you want to fetch only one row, you can use cur.fetchrow(), which gives only one row, and you don't need to iterate that for row in rows: row_new = row[0].split(\n, ) row = .join(row_new) as Benn Finney wrote, the use of split() above is wrong. I think you need some kinf of this: .join([f.strip() for f in row[0].replace(\n, ).split()]) but it depends what you want. Cheers: Ervin -- https://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb Help
Hello, On Tue, Sep 17, 2013 at 05:35:30PM +1000, Ben Finney wrote: Venkat Addala venkat.bof...@gmail.com writes: rows = cur.fetchall() for row in rows: You never use ‘rows’ for anything else, so you may as well forget it and just iterate directly over the return value:: for row in cur.fetchall(): and what if in body of iteration there is another fetchall()? :) I mean: for row in cur.fetchall(): do_domething() cur.execute(SELECT * FROM another_table) Cheers: Ervin -- https://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb Help
Hello, On Tue, Sep 17, 2013 at 07:22:50PM +1000, Ben Finney wrote: Ervin Hegedüs airw...@gmail.com writes: Hello, On Tue, Sep 17, 2013 at 05:35:30PM +1000, Ben Finney wrote: for row in cur.fetchall(): and what if in body of iteration there is another fetchall()? :) Yes, what if? Each call to ‘fetchall’ returns a sequence of rows. I don't see your point. yep', sorry, I'm confused. I just remember - maybe badly - once I used this form, and the 'row' continues the new sequence... But now I tried, and I see this presumtion is wrong. Sorry for disturbing, and thanks :) a. -- https://mail.python.org/mailman/listinfo/python-list
Re: detect key conflict in a JSON file
Hello, On Wed, May 29, 2013 at 03:41:50PM +0200, Jabba Laci wrote: The real answer here is that JSON is probably not the best choice for large files that get hand-edited. For data that you intend to hand-edit a lot, YAML might be a better choice. Currently the value of the second key silently overwrites the value of the first. Thanks but how would it be different with YAML? Can YAML check duplicate keys? no, I think it can't, I'm using yaml a few months ago, as I noticed, the last key prevail. How to process (read) YAML files in Python? Can you give me some hints how to get started? All I need is read it in and create a dictionary of it. import yaml struct = yaml.load(file(yamlfile.yml 'r')) and struct will be a Python dictionary, As I know, yaml modul is not part of standard Python library, but most Linux systems has package, eg. Debian/Ubuntu has a python-yaml. Cheers: a. -- http://mail.python.org/mailman/listinfo/python-list
Re: send email with bcc
Hello, On Fri, Nov 30, 2012 at 12:25:37PM -0800, Ed wrote: # Send the email smtp.sendmail(sender, [to] + bcc, msg.as_string()) The above generates the following error: Traceback (most recent call last): File /opt/batch/ebtest/example4.py, line 46, in module smtp.sendmail(sender, [to] + bcc, msg.as_string()) didn't you forgot to attach the reason of the error, I mean what is the Exception type? Other iterations of the code have worked without error, but do not send mail to the BCC recipient. you could't concatenate a list and a string at this way. You can do that one of these: l = ['foo'] s = 'bar' l.append(s) or n = l+[s] a. -- I � UTF-8 -- http://mail.python.org/mailman/listinfo/python-list
Re: Daemon loses __file__ reference after a while.
hello, On Tue, Jul 24, 2012 at 04:48:42AM -0700, ivdn...@gmail.com wrote: Hello, I have a daemon process that runs for a considerable amount of time (weeks on end) without any problems. At some point I start getting the exception: Exception info: Traceback (most recent call last): File scheduler.py, line 376, in applyrule result = execrule(rule_code) File scheduler.py, line 521, in execrule rulepath = os.path.dirname(__file__)+/+'/'.join(rule['modules'])+/+rule['rulename'] NameError: name '__file__' is not defined This section of the code is executed in this process *all the time*, but suddenly stops working. I have been searching for similar issues online, but only come accross people having problems because they run the script interactively. This is not the case here. could you send the relevant part of the code? I mean: how do you daemonize your process? I am running python from a virtual-env installation from a stock Red Hat EL 6.2 installation: (virtual-env)[user@host ~]$ python --version Python 2.6.6 (virtual-env)[user@host ~]$ cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.2 (Santiago) If you use fork(), it drops all file descriptors, and creates new ones - may be then loss the __file__...? a. -- I � UTF-8 -- http://mail.python.org/mailman/listinfo/python-list
Re: how do i merge two sequence
hi, On Wed, Apr 18, 2012 at 10:41:00PM +0200, Peter Otten wrote: Python Email wrote: how do i merge two seqs alernative; (xyz, 7890) output: x7y8z90 import itertools .join(a+b for a, b in itertools.izip_longest(xyz, 7890, fillvalue=)) 'x7y8z90' why is this better than simple .join((xyz, 7890))? thanks: a. -- I � UTF-8 -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking PDF module
hello, On Fri, Jun 24, 2011 at 09:59:17AM -0700, Cameron Laird wrote: Hegedüs Ervin, it's quite likely that ReportLab will be a good technical fit for you. it's a good news :) Are you in a position to pay licensing fees for advanced features? no :( Do you have any requirements to *merge* PDF instances? may be, hope I don't, but currently I don't know, How stringent are your performance requirements? there isn't stringent... thanks: a. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree XML parsing problem
hello, On Thu, Apr 28, 2011 at 07:57:28AM +0200, Stefan Behnel wrote: So, I started change the codepage mark of xml: ?xml version=1.0 encoding=UTF-8 ? - same result ?xml version=1.0 encoding=ISO-8859-2 ? - same result ?xml version=1.0 encoding=ISO-8859-1 ? - same result You probably changed this in an editor that supports XML and thus saves the file in the declared encoding. no. I've saved the XML as UTF8, and didn't change the _file_ encoding - just modified the XML header, nothing else... (I'm using Geany - it doesn't realize what user wrote in file, just can save file as another encodign, when user choose one) Switching between the three by simply changing the first line (the XML declaration) and not adapting the encoding of the document itself would otherwise not yield the same result for the document given above. yes, that's what I wrote exactly. a. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need your help
hello, Here I need some help. #encoding=utf-8 #moudle a.py def a(): print function a! #encoding=utf-8 #moudle b.py def b(): print function b! #encoding=utf-8 #moudle c.py import a import b def c(): a.a() b.b() Here in function c,How can i record all the information printed by a and b with out modifying moudle a and b? I want to output all the printed information into a text file. Need your help, thanks a lot! sounds you want something like this: #!/usr/bin/python import a import b import sys import StringIO output = StringIO.StringIO() def c(): # save default stdout tout = sys.stdout # redirect stdout to StringIO object sys.stdout = output # a.a() prints their output to StringIO object a.a() # back up default stdout sys.stdout = tout # print StringIO object value print retval:, output.getvalue() c() a. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need your help
hello, On Thu, Apr 28, 2011 at 01:20:16PM +0200, Ervin Hegedüs wrote: #!/usr/bin/python import a import b import sys import StringIO output = StringIO.StringIO() def c(): # save default stdout tout = sys.stdout # redirect stdout to StringIO object sys.stdout = output # a.a() prints their output to StringIO object a.a() # back up default stdout sys.stdout = tout # print StringIO object value print retval:, output.getvalue() c() sorry, this is a littlebit more abstact example, it gives a StringIO object, instead of write to a file - of course, you can do what you want with that string... a. -- http://mail.python.org/mailman/listinfo/python-list
Py_INCREF() incomprehension
Hello Python users, I'm working on a Python module in C - that's a cryptographic module, which uses a 3rd-party lib from a provider (a bank). This module will encrypt and decrypt the messages for the provider web service. Here is a part of source: static PyObject* mycrypt_encrypt(PyObject *self, PyObject *args) { int cRes = 0; int OutLen = 0; char * url; char * path; if (!PyArg_ParseTuple(args, ss, url, path)) { return NULL; } OutLen = strlen(url)*4; outdata=calloc(OutLen, sizeof(char)); if (!outdata) { handle_err(UER_NOMEM); return NULL; } cRes = ekiEncodeUrl (url, strlen(url)+1, outdata, OutLen, 1, path); if (cRes == 0) { return Py_BuildValue(s, outdata); } else { handle_err(cRes); return NULL; } return Py_None; } where ekiEncodeUrl is in a 3rd-party library. I should call this function from Python like this: import mycrypt message = PID=IEB0001MSGT=10TRID=00012345678 crypted = mycrypt(mymessage, /path/to/key); Everything works fine, but sorry for the recurrent question: where should I use the Py_INCREF()/Py_DECREF() in code above? Thank you, cheers: a. -- http://mail.python.org/mailman/listinfo/python-list