Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 7/6/2013 4:01 πμ, Cameron Simpson wrote:
On 06Jun2013 11:46, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
wrote:
| Τη Πέμπτη, 6 Ιουνίου 2013 3:44:52 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
έγραψε:
| > py> s = '999-Eυχή-του-Ιησού'
| > py> bytes_as_utf8 = s.encode('utf-8')
| > py> t = bytes_as_utf8.decode('iso-8859-7', errors='replace')
| > py> print(t)
| > 999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
|
| errors='replace' mean dont break in case or error?
Yes. The result will be correct for correct iso-8859-7 and slightly mangled
for something that would not decode smoothly.
How can it be correct? We have encoded out string in utf-8 and then we
tried to decode it as greek-iso? How can this possibly be correct?
| You took the unicode 's' string you utf-8 bytestringed it.
| Then how its possible to ask for the utf8-bytestring to decode
| back to unicode string with the use of a different charset that the
| one used for encoding and thsi actually printed the filename in
| greek-iso?
It is easily possible, as shown above. Does it make sense? Normally
not, but Steven is demonstrating how your "mv" exercises have
behaved: a rename using utf-8, then a _display_ using iso-8859-7.
Same as above, i don't understand it at all, since different
charsets(encodings) used in the encode/decode process.
|
| a) WHAT does it mean when a linux system is set to use utf-8?
It means the locale settings _for the current process_ are set for
UTF-8. The "locale" command will show you the current state.
That means that, when a linux application needs to saved a filename to
the linux filesystem, the app checks the filesytem's 'locale', so to
encode the filename using the utf-8 charset ?
And likewise when a linux application wants to decode a filename is also
checking the filesystem's 'locale' setting so to know what charset must
use to decode the filename correctly back to the original string?
So locale is used for filesystem itself and linux apps to know how to
read(decode) and write(enode) filenames from/into the system's hdd?
| c) WHAT happens when the two of them try to work together?
If everything matches, it is all good. If the locales do not match,
the mismatch will result in an undesired bytes<->characters
encode/decode step somewhere, and something will display incorrectly
or be entered as input incorrectly.
Cant quite grasp the idea:
local end: Win8, locale = greek-iso
remote end: CentOS 6.4, locale = utf-8
FileZilla by default uses "do not know what charset" to upload filenames
Putty by default uses greek-iso to display filenames
WHAT someone can expect to happen when all of the above work together?
Mess of course, but i want to hear in detail each step of the mess as it
emerges.
--
Webhost && Weblog
--
http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
[email protected] writes: > File "files.py", line 75 > os.rename( filepath_bytes filepath.encode('utf-8') ) > ^ > SyntaxError: invalid syntax > > I am seeign the caret pointing at filepath but i cant follow what it > tries to tell me. As already explained, often a SyntaxError is introduced by *preceeding* "text", so you must look at your code with a "wider eye". > This rename statement tries to convert the greek byted filepath to > utf-8 byted filepath. Yes: and that usually imply that the *function* accepts (at least) *two* arguments, specifically the source and the target names, right? How many arguments are you actually giving to the os.rename() function above? > I can't see whay this is wrong though. Try stronger, I won't be give you further indications to your SyntaxErrors, you *must* learn how to detect and fix those by yourself. ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. [email protected] | -- Fortunato Depero, 1929. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
Τη Παρασκευή, 7 Ιουνίου 2013 9:46:53 π.μ. UTC+3, ο χρήστης Chris Angelico έγραψε: > On Fri, Jun 7, 2013 at 4:35 PM, wrote: > > > Yes, but but 'putty' seems to always forget when i tell it to use utf8 for > > displaying and always picks up the Win8's default charset and it doesnt > > have a save options dialog. I cant always remember to switch to utf8 > > charset or renaming all the time from termnal so many greek filenames. > > > > > > I use PuTTY too (though that'll change when I next upgrade Traal, as > > I'll no longer have any Windows clients), and it's set to UTF-8 in the > > Winoow|Translation page. Far as I know, those settings are all saved > > into the Saved Sessions settings, back on the Session page. > > > > ChrisA Session settings afaik is for putty to remember hosts to connect to, not terminal options. I might be worng though. No matter how many times i change its options next time i run it always defaults back. I'll google Traal right now. You should also take o look on 'Secure Shell' extension for Chrome i just found out. Seems a great plugin for Chrome. You'll definately like it, i did! -- http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
Any other ideas guys? I can output for you any command you ask me too, so you have a better understanding about this suexec thing. I have checked the path to the error log file too, it's not just enough that the file is accessible for a user, the whole path to the file needs to have the right permissions. ls -ld /var/log/httpd ls -ld /var/log both of those needs to have xr for group and others (as far as i know x allows a user to get into the directory, r gives the user the ability to list files in the directory). So, --- [email protected] [~]# ls -ld /var/log/ drwxr-xr-x 8 root root 4096 Jun 6 08:56 /var/log// root@nikos [~]# ls -ld /var/log/httpd/ drw-rw-rw- 2 root root 4096 Jun 6 22:03 /var/log/httpd// root@nikos [~]# chmod 666 /var/log/httpd/ root@nikos [~]# ls -l /var/log/httpd/ total 8 drw-rw-rw- 2 root root 4096 Jun 6 22:03 ./ drwxr-xr-x 8 root root 4096 Jun 7 10:02 ../ -rwxr-xr-x 1 nobody nobody0 Apr 14 08:57 error_log* -rwxr-xr-x 1 nobody nobody0 Jun 1 02:52 suexec_log* -- [email protected] [~]# ls -ld /usr/local/apache/logs/ drwxr-xr-x 2 root root 4096 Jun 6 08:14 /usr/local/apache/logs// [email protected] [~]# ls -ld /usr/local/apache/ drwxr-xr-x 17 root root 4096 May 16 22:46 /usr/local/apache// root@nikos [~]# chown nobody:nobody /usr/local/apache/logs/suexec_log root@nikos [~]# ls -l /usr/local/apache/logs/suexec_log -rw-rw-r-- 1 nobody nobody 678180 Jun 7 08:29 /usr/local/apache/logs/suexec_log root@nikos [~]# -- Because i'm a bit confused both 'suexec' files need to change? And what are the differences of them? Why two 'suexec' logs appear? httpd != Apache ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On Fri, Jun 7, 2013 at 5:08 PM, Νικόλαος Κούρας wrote: > I'll google Traal right now. The one thing you're actually willing to go research, and it's actually something that won't help you. Traal is the name of my personal laptop. Spend your Googletrons on something else. :) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
Τη Παρασκευή, 7 Ιουνίου 2013 10:09:29 π.μ. UTC+3, ο χρήστης Lele Gaifax έγραψε:
> As already explained, often a SyntaxError is introduced by *preceeding*
> "text", so you must look at your code with a "wider eye".
That what i ahte aabout error reporting. You have some syntax error someplace
and error reports you another line, so you have to check the whole code again.
Well i just did, i see no syntactical errors.
> Yes: and that usually imply that the *function* accepts (at least) *two*
> arguments, specifically the source and the target names, right? How many
> arguments are you actually giving to the os.rename() function above?
i'm giving it two.
os.rename( filepath_bytes filepath.encode('utf-8') )
1st = filepath_bytes
2nd = filepath.encode('utf-8')
Source and Target respectively.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On Jun 7, 2013, at 8:32, Νικόλαος Κούρας wrote:
> Τη Παρασκευή, 7 Ιουνίου 2013 10:09:29 π.μ. UTC+3, ο χρήστης Lele Gaifax
> έγραψε:
>
>> As already explained, often a SyntaxError is introduced by *preceeding*
>> "text", so you must look at your code with a "wider eye".
>
> That what i ahte aabout error reporting. You have some syntax error someplace
> and error reports you another line, so you have to check the whole code again.
> Well i just did, i see no syntactical errors.
>
>> Yes: and that usually imply that the *function* accepts (at least) *two*
>> arguments, specifically the source and the target names, right? How many
>> arguments are you actually giving to the os.rename() function above?
>
> i'm giving it two.
> os.rename( filepath_bytes filepath.encode('utf-8')
Missing comma, which is, after all, just a matter of syntax so it can't matter,
right?
>
> 1st = filepath_bytes
> 2nd = filepath.encode('utf-8')
>
> Source and Target respectively.
> --
> http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
Finally no suexec erros any more after chown all log files to nobody:nobody and thei corresponding paths. Now the error has been transformed to: [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/koukos.py' failed [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] File does not exist: /home/nikos/public_html/500.shtml but from interpretor view: [email protected] [~/www/cgi-bin]# python koukos.py Set-Cookie: nikos=admin; expires=Mon, 02 Jun 2014 07:50:18 GMT; Path=/ Content-type: text/html; charset=utf-8 ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ ΣΕ ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!! (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/koukos.py' failed Can find what? koukos.py is there inside the cg-bin dir with 755 perms. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 7/6/2013 10:42 πμ, Michael Weylandt wrote:
os.rename( filepath_bytes filepath.encode('utf-8')
Missing comma, which is, after all, just a matter of syntax so it can't matter,
right?
I doubted that os.rename arguments must be comma seperated.
But ater reading the docs.
s.rename(/src/,/dst/)
Rename the file or directory/src/to/dst/. If/dst/is a
directory,OSError
will
be raised. On Unix, if/dst/exists and is a file, it will be replaced
silently if the user has permission. The operation may fail on some
Unix flavors if/src/and/dst/are on different filesystems. If
successful, the renaming will be an atomic operation (this is a
POSIX requirement). On Windows, if/dst/already exists,OSError
will
be raised even if it is a file; there may be no way to implement an
atomic rename when/dst/names an existing file.
Availability: Unix, Windows.
Indeed it has to be:
os.rename( filepath_bytes, filepath.encode('utf-8')
'mv source target' didn't require commas so i though it was safe to assume that
os.rename did not either.
I'am happy to announce that after correcting many idiotic error like commas,
missing colons and declaring of variables, this surrogate erro si the last i
get.
I still dont understand what surrogate means. In english means replacement.
Here is the code:
#
# Collect filenames of the path dir as bytes
filename_bytes = os.listdir( b'/home/nikos/public_html/data/apps/' )
# Iterate over all filenames in the path dir
for filename in filename_bytes:
# Compute 'path/to/filename' in bytes
filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename'
try:
filepath = filepath_bytes.decode('utf-8')
except UnicodeDecodeError:
try:
filepath = filepath_bytes.decode('iso-8859-7')
# Rename current filename from greek bytes => utf-8
bytes
os.rename( filepath_bytes, filepath.encode('utf-8') )
except UnicodeDecodeError:
print( '''I give up! This filename is unreadable! ''')
#
# Get filenames of the apps directory as unicode
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
# Load'em
for filename in filenames:
try:
# Check the presence of a file against the database and insert
if it doesn't exist
cur.execute('''SELECT url FROM files WHERE url = %s''',
(filename,) )
data = cur.fetchone()#filename is unique, so should
only be one
if not data:
# First time for file; primary key is automatic, hit is
defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit)
VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )
#
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
filenames = ()
# Build a set of 'path/to/filename' based on the objects of path dir
for filename in filenames:
filenames.add( filename )
# Delete spurious
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()
# Check database's filenames against path's filenames
for filename in data:
if filename not in filenames:
cur.execute('''DELETE FROM files WHERE url = %s''', (filename,)
)
=
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173] File
"/home/nikos/public_html/cgi-bin/files.py", line 88, in
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173]
cur.execute('''SELECT url FROM files WHERE url = %s''', filename )
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173] File
"/usr/local/lib/python3.3/site-packages/PyMySQL3-0.5-py3.3.egg/pymysql/cursors.py",
line 108, in execute
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173] query =
query.encode(charset)
[Fri Jun 07 11:08:17 2013] [error] [client 79.103.41.173] UnicodeEncodeError:
'utf-8' codec can't encode character '\\udcce' in position 35: surrogates not
allowed
--
Webhost && Weblog
--
http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On Fri, Jun 7, 2013 at 9:10 AM, Νικόλαος Κούρας wrote:
> On 7/6/2013 10:42 πμ, Michael Weylandt wrote:
>
>>> os.rename( filepath_bytes filepath.encode('utf-8')
>
>> Missing comma, which is, after all, just a matter of syntax so it can't
>> matter, right?
>
> I doubted that os.rename arguments must be comma seperated.
All function calls in Python require commas if you are putting in more
than one argument. [0]
> But ater reading the docs.
>
> s.rename(src, dst)
>
> Rename the file or directory src to dst. If dst is a directory, OSError will
> be raised. On Unix, if dst exists and is a file, it will be replaced
> silently if the user has permission. The operation may fail on some Unix
> flavors if src and dst are on different filesystems. If successful, the
> renaming will be an atomic operation (this is a POSIX requirement). On
> Windows, if dst already exists, OSError will be raised even if it is a file;
> there may be no way to implement an atomic rename when dst names an existing
> file.
>
> Availability: Unix, Windows.
>
> Indeed it has to be:
>
> os.rename( filepath_bytes, filepath.encode('utf-8')
Parenthesis missing here as well.
>
> 'mv source target' didn't require commas so i though it was safe to assume
> that os.rename did not either.
>
That's for shell programming -- different language entirely.
The surrogate business is back to Unicode, which ain't my specialty so
I'll leave that to more able programmers.
MW
[0] You could pass multiple arguments by way of a tuple or dictionary
using */** but if you want arguments that aren't in the container
being passed, you're back to needing commas.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07Jun2013 11:10, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
wrote:
| On 7/6/2013 10:42 πμ, Michael Weylandt wrote:
| >os.rename( filepath_bytes filepath.encode('utf-8')
| >Missing comma, which is, after all, just a matter of syntax so it can't
matter, right?
|
| I doubted that os.rename arguments must be comma seperated.
Why?
Every other python function separates arguments with commas.
| 'mv source target' didn't require commas so i though it was safe to assume
that os.rename did not either.
"mv" is shell syntax.
os.rename is Python syntax.
Two totally separate languages.
--
Cameron Simpson
Cynic, n. A blackguard whose faulty vision sees things as they are, not as
they ought to be.
Ambrose Bierce (1842-1914), U.S. author. The Devil's Dictionary (1881-1906).
--
http://mail.python.org/mailman/listinfo/python-list
Re: Thread-safe way to prevent decorator from being nested
Michael wrote: > I'm writing a decorator that I never want to be nested. Following from the > answer on my StackOverflow question > (http://stackoverflow.com/a/16905779/106244), I've adapted it to the > following. > > Can anyone spot any issues with this? It'll be run in a multi-threaded > environment serving Django requests and also be a part of Celery tasks. I'm not sure I understand what you are trying to do, but this > if not within_special_wrapper(): > with flag(): looks suspiciously like race condition. >thread_safe_globals = threading.local() I'm not an expert in the area, but I think you need a lock, something like class NestingError(Exception): pass nest_lock = threading.Lock() def my_special_wrapper(f): @wraps(f) def internal(*args, **kwargs): if nest_lock.acquire(False): # non-blocking try: f(*args, **kwargs) finally: nest_lock.release() else: raise NestingError return internal -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07Jun2013 09:56, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
wrote:
| On 7/6/2013 4:01 πμ, Cameron Simpson wrote:
| >On 06Jun2013 11:46, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
wrote:
| >| Τη Πέμπτη, 6 Ιουνίου 2013 3:44:52 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
έγραψε:
| >| > py> s = '999-Eυχή-του-Ιησού'
| >| > py> bytes_as_utf8 = s.encode('utf-8')
| >| > py> t = bytes_as_utf8.decode('iso-8859-7', errors='replace')
| >| > py> print(t)
| >| > 999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
| >|
| >| errors='replace' mean dont break in case or error?
| >
| >Yes. The result will be correct for correct iso-8859-7 and slightly mangled
| >for something that would not decode smoothly.
|
| How can it be correct? We have encoded out string in utf-8 and then
| we tried to decode it as greek-iso? How can this possibly be
| correct?
Ok, not correct. But consistent. Safe to call.
If it is a valid iso-8859-7 sequence (which might cover everything,
since I expect it is an 8-bit 1:1 mapping from bytes values to a
set of codepoints, just like iso-8859-1) then it may decode to the
"wrong" characters, but the reverse process (characters encoded as
bytes) should produce the original bytes. With a mapping like this,
errors='replace' may mean nothing; there will be no errors because
the only Unicode characters in play are all from iso-8859-7 to start
with. Of course another string may not be safe.
| >| You took the unicode 's' string you utf-8 bytestringed it.
| >| Then how its possible to ask for the utf8-bytestring to decode
| >| back to unicode string with the use of a different charset that the
| >| one used for encoding and thsi actually printed the filename in
| >| greek-iso?
| >
| >It is easily possible, as shown above. Does it make sense? Normally
| >not, but Steven is demonstrating how your "mv" exercises have
| >behaved: a rename using utf-8, then a _display_ using iso-8859-7.
|
| Same as above, i don't understand it at all, since different
| charsets(encodings) used in the encode/decode process.
Visually, the names will be garbage. And if you go:
mv '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' '999-Eυχή-του-Ιησού.mp3'
while using the iso-8859-7 locale, the wrong thing will occur
(assuming it even works, though I think it should because all these
characters are represented in iso-8859-7, yes?)
Why?
In the iso-8859-7 locale, your (currently named under an utf-8
regime) file looks like '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' (because the
Unicode byte sequence maps to those characters in iso-8859-7). Why
you issue the about "mv" command, the new name will be the _iso-8859-7_
bytes encoding for '999-Eυχή-του-Ιησού.mp3'. Which, under an utf-8
regime will decode to _other_ characters.
If you want to repair filenames, by which I mean, cause them to be correctly
encoded for utf-8, you are best to work in utf-8 (using "mv" or python).
Of course, the badly named files will then look wrong in your listing.
If you _know_ the filenames were written using iso-8859-7 encoding, and that
the names are "right" under that encoding, you can write python code to rename
them to utf-8.
Totally untested example code:
import sys
from binascii import hexlify
for bytename in os.listdir( b'.' ):
unicode_name = bytename.decode('iso-8859-7')
new_bytename = unicode_name.encode('utf-8')
print("%s: %s => %s" % (unicode_name, hexlify(bytename),
hexlify(new_bytename)), file=sys.stderr)
os.rename(bytename, new_bytename)
That code should not care what locale you are using because it uses
bytes for the file calls and is explicit about the encoding/decoding
steps.
| >| a) WHAT does it mean when a linux system is set to use utf-8?
| >
| >It means the locale settings _for the current process_ are set for
| >UTF-8. The "locale" command will show you the current state.
|
| That means that, when a linux application needs to saved a filename
| to the linux filesystem, the app checks the filesytem's 'locale', so
| to encode the filename using the utf-8 charset ?
At the command line, many will not. They'll just read and write bytes.
Some will decode/encode. Those that do, should by default use the
current locale.
But broadly, it is GUI apps that care about this because they must
translate byte sequences to glyphs: images of characters. So plenty
of command line tools do not need to care; the terminal application
is the one that presents the names to you; _it_ will decode them
for display. And it is the terminal app that translates your
keystrokes into bytes to feed to the command line.
NOTE: it is NOT the filesystem's locale. It is the current process'
locale, which is deduced from environment variables (which have
defaults if they are not set).
Under Windows I believe filesystems have locales; this can prevent
you storing some files on some filesystems on Windows, because the
filesystem doesn't cope. UNIX just takes bytes.
| And likewise when a linux application wants to decode a filename is
| also checking the filesystem's 'locale' setting so to know what
|
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On Jun 7, 6:53 pm, Cameron Simpson wrote: > Experiment: > > LC_ALL=C ls -b > LC_ALL=utf-8 ls -b > LC_ALL=iso-8859-7 ls -b > > And the Terminal itself is decoding the output for display, and > encoding your input keystrokes to feed as input to the command > line. This reminded me of something I saw on stackoverflow recently: http://stackoverflow.com/questions/11735363/python3-unicodeencodeerror-only-when-run-from-crontab Script would run from shell but not from crontab, as the crontab environment had different locale settings. Solution was to prepend the correct LC_CTYPE to the command in the crontab. Would it be similar for httpd processes? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems with serial port interface
Sorry for my quote, but do you have any suggestion? Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, [email protected] ha scritto: > Hi, > > i'm programming in python for the first time: i want to create a serial port > reader. I'm using python3.3 and pyQT4; i'm using also pyserial. > > Below a snippet of the code: > > > > class CReader(QThread): > > def start(self, ser, priority = QThread.InheritPriority): > > self.ser = ser > > QThread.start(self, priority) > > self._isRunning = True > > self.numData=0; > > > > def run(self): > > print("Enter Creader") > > while True: > > if self._isRunning: > > try: > > data = self.ser.read(self.numData) > > n = self.ser.inWaiting() > > if n: > > data = self.ser.read(n) > > self.emit(SIGNAL("newData(QString)"), > data.decode('cp1252', 'ignore')) > > self.ser.flushInput() > > except: > > pass > > else: > > return > > > > def stop(self): > > self._isRunning = False > > self.wait() > > > > This code seems work well, but i have problems in this test case: > > > > +baud rate:19200 > > +8/n/1 > > +data transmitted: 1 byte every 5ms > > > > After 30seconds (more or less) the program crashes: seems a buffer problem, > but i'm not really sure. > > What's wrong? > > Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems with serial port interface
[email protected] wrote: > Sorry for my quote, > but do you have any suggestion? >> After 30seconds (more or less) the program crashes: seems a buffer >> problem, but i'm not really sure. >> >> What's wrong? I don't use qt or pyserial myself, but your problem description is too vague anyway. Some random remarks: Does your script segfault or do you get a traceback? If the latter, post it. As you are using two external libraries, can you limit the problem to a single one? For example: temporarily replace pyserial with a file. Do the problems persist? What happens if you remove the bare try: ... except: pass ? It may hide useful information. Finally, can you make a self-contained example that we can run? Make it as simple as possible. I'd start with something like class CReader(QThread): def __init__(self, ser): self.ser = ser def run(self): while True: data = self.ser.read(1) if data: n = self.ser.inWaiting() if n: data += self.ser.read(n) text = data.decode('cp1252', 'ignore') print(text) # adding the following would be the next step #self.emit(SIGNAL("newData(QString)"), text) and once you have something that does run you can gradually increase complexity until it breaks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
Τη Παρασκευή, 7 Ιουνίου 2013 11:53:04 π.μ. UTC+3, ο χρήστης Cameron Simpson
έγραψε:
> On 07Jun2013 09:56, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
> wrote:
>
> | On 7/6/2013 4:01 πμ, Cameron Simpson wrote:
>
> | >On 06Jun2013 11:46, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
> wrote:
>
> | >| Τη Πέμπτη, 6 Ιουνίου 2013 3:44:52 μ.μ. UTC+3, ο χρήστης Steven D'Aprano
> έγραψε:
>
> | >| > py> s = '999-Eυχή-του-Ιησού'
>
> | >| > py> bytes_as_utf8 = s.encode('utf-8')
>
> | >| > py> t = bytes_as_utf8.decode('iso-8859-7', errors='replace')
>
> | >| > py> print(t)
>
> | >| > 999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ
>
> | >|
>
> | >| errors='replace' mean dont break in case or error?
>
> | >
>
> | >Yes. The result will be correct for correct iso-8859-7 and slightly mangled
>
> | >for something that would not decode smoothly.
>
> |
>
> | How can it be correct? We have encoded out string in utf-8 and then
>
> | we tried to decode it as greek-iso? How can this possibly be
>
> | correct?
> If it is a valid iso-8859-7 sequence (which might cover everything,
> since I expect it is an 8-bit 1:1 mapping from bytes values to a
> set of codepoints, just like iso-8859-1) then it may decode to the
> "wrong" characters, but the reverse process (characters encoded as
> bytes) should produce the original bytes. With a mapping like this,
> errors='replace' may mean nothing; there will be no errors because
> the only Unicode characters in play are all from iso-8859-7 to start
> with. Of course another string may not be safe.
> Visually, the names will be garbage. And if you go:
> mv '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' '999-Eυχή-του-Ιησού.mp3'
> while using the iso-8859-7 locale, the wrong thing will occur
> (assuming it even works, though I think it should because all these
> characters are represented in iso-8859-7, yes?)
All the rest you i understood only the above quotes its still unclear to me.
I cant see to understand it.
Do you mean that utf-8, latin-iso, greek-iso and ASCII have the 1st 0-127
codepoints similar?
For example char 'a' has the value of '65' for all of those character sets?
Is hat what you mean?
s = 'a' (This is unicode right? Why when we assign a string to a variable
that string's type is always unicode and does not automatically become utf-8
which includes all available world-wide characters? Unicode is something
different that a character set? )
utf8_byte = s.encode('utf-8')
Now if we are to decode this back to utf8 we will receive the char 'a'.
I beleive same thing will happen with latin, greek, ascii isos. Correct?
utf8_a = utf8_byte.decode('iso-8859-7')
latin_a = utf8_byte.decode('iso-8859-1')
ascii_a = utf8_byte.decode('ascii')
utf8_a = utf8_byte.decode('iso-8859-7')
Is this correct?
All of those decodes will work even if the encoded bytestring was of utf8 type?
The characters that will not decode correctly are those that their codepoints
are greater that > 127 ?
for example if s = 'α' (greek character equivalent to english 'a')
Is this what you mean?
Now back to my almost ready files.py script please:
#
# Collect filenames of the path dir as bytes
greek_filenames = os.listdir( b'/home/nikos/public_html/data/apps/' )
for filename in greek_filenames:
# Compute 'path/to/filename' in bytes
greek_path = b'/home/nikos/public_html/data/apps/' + b'filename'
try:
filepath = greek_path.decode('iso-8859-7')
# Rename current filename from greek bytes --> utf-8 bytes
os.rename( greek_path, filepath.encode('utf-8') )
except UnicodeDecodeError:
# Since its not a greek bytestring then its a proper utf8
bytestring
filepath = greek_path.decode('utf-8')
#
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
# Load'em
for filename in filenames:
try:
# Check the presence of a file against the database and insert
if it doesn't exist
cur.execute('''SELECT url FROM files WHERE url = %s''',
filename )
data = cur.fetchone()
if not data:
# First time for file; primary key is automatic, hit is
defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit)
VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )
#
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
filepaths = ()
# Build a set of 'path/to/filename' based on the objects of path dir
for filename in filenames:
filepaths.add( filename )
# Delete spurious
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()
# Check database's filenames against path's filen
Contents of python magazine
Hello pythonistas, ☺ Some application made of python(command line program or a GUI program) that we make using a python module(planning to cover as many modules as possible) ie; of included batteries of python. ☺ Python in Security ie; Some scripts that we can write in python like a dictionary attack tool ☺ One for covering Tkinter. ☺ One small Game using python. ☺ Python Facts,Python Quotes, Python PersonalitY (a python personnel bio or an interview), Python video(about a python video from Python Conferences) ☺ Python articles Guest as well as our own. ☺ Python Tutorials. ☺ Python Place( A place where python is being used for production). ☺ If we are not overloading magazine we would also cover about Python tools like say Ninja-IDE(an IDE for Python extensively written in Python). After a few days we are planning to cover ☺ Python in cloud.(Open Stack) ☺ Network Programming.(probably Twisted framework) ☺ Database programming with SQLite. ☺ Python Puzzles(some programming Questions). ☺ Python Webframework probably Django. We welcome further suggestions from the community. Regards, DRJ and Team. -- http://mail.python.org/mailman/listinfo/python-list
EuroPython 2014/2015 Conference Team - Reminder: Call for Proposals
This is a reminder to all teams who want to submit a proposal for running the next EuroPython in 2014 and 2015. Proposals must be sent in before Friday, June 14th, i.e. in less than one week. If you have questions, please feel free to contact the EuroPython Society board at [email protected]. We're looking forward to hearing from you :-) Here's a copy of the original announcement email with the link to the CFP document: """ The EuroPython Society (EPS) is happy to announce the Call for Proposals for the EuroPython Conference in 2014 and 2015. This Call for Proposals is meant to collect proposals from teams that volunteer for organizing the EuroPython conference in 2014-2015. The Call for Proposals document containing all the details and information about the proposals and selection process can be found here: https://docs.google.com/file/d/0B8YBdLoQM_6fbVpuM2ZWUGp3Slk/edit?usp=sharing If you are part of a great team organizing amazing Python events you could be the team organizing the next EuroPython! Please also forward this Call for Proposals to anyone that you feel may be interested. The Call for Proposals will run until Friday, June 14th. Proposals must be submitted to [email protected] before that day, and must adhere the requirements specified in the CFP document. """ Regards, -- Marc-Andre Lemburg Director EuroPython Society http://www.europython.eu/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems with serial port interface
On 07/06/2013 11:17, [email protected] wrote: Sorry for my quote, but do you have any suggestion? Il giorno martedì 4 giugno 2013 23:25:21 UTC+2, [email protected] ha scritto: Hi, i'm programming in python for the first time: i want to create a serial port reader. I'm using python3.3 and pyQT4; i'm using also pyserial. Below a snippet of the code: class CReader(QThread): def start(self, ser, priority = QThread.InheritPriority): self.ser = ser QThread.start(self, priority) self._isRunning = True self.numData=0; def run(self): print("Enter Creader") while True: if self._isRunning: try: data = self.ser.read(self.numData) n = self.ser.inWaiting() if n: data = self.ser.read(n) self.emit(SIGNAL("newData(QString)"), data.decode('cp1252', 'ignore')) self.ser.flushInput() except: pass else: return def stop(self): self._isRunning = False self.wait() This code seems work well, but i have problems in this test case: +baud rate:19200 +8/n/1 +data transmitted: 1 byte every 5ms After 30seconds (more or less) the program crashes: seems a buffer problem, but i'm not really sure. What's wrong? Using a "bare except" like this: try: ... except: ... is virtually always a bad idea. The only time I'd ever do that would be, say, to catch something, print a message, and then re-raise it: try: ... except: print("Something went wrong!") raise Even then, catching Exception would be better than a bare except. A bare except will catch _every_ exception, including NameError (which would mean that it can't find a name, possibly due to a spelling error). A bare except with pass, like you have, is _never_ a good idea. Python might be trying to complain about a problem, but you're preventing it from doing so. Try removing the try...except: pass and let Python tell you if it has a problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07/06/2013 12:53, Νικόλαος Κούρας wrote:
[snip]
#
# Collect filenames of the path dir as bytes
greek_filenames = os.listdir( b'/home/nikos/public_html/data/apps/' )
for filename in greek_filenames:
# Compute 'path/to/filename' in bytes
greek_path = b'/home/nikos/public_html/data/apps/' + b'filename'
try:
This is a worse way of doing it because the ISO-8859-7 encoding has 1
byte per codepoint, meaning that it's more 'tolerant' (if that's the
word) of errors. A sequence of bytes that is actually UTF-8 can be
decoded as ISO-8859-7, giving gibberish.
UTF-8 is less tolerant, and it's the encoding that ideally you should
be using everywhere, so it's better to assume UTF-8 and, if it fails,
try ISO-8859-7 and then rename so that any names that were ISO-8859-7
will be converted to UTF-8.
That's the reason I did it that way in the code I posted, but, yet
again, you've changed it without understanding why!
filepath = greek_path.decode('iso-8859-7')
# Rename current filename from greek bytes --> utf-8 bytes
os.rename( greek_path, filepath.encode('utf-8') )
except UnicodeDecodeError:
# Since its not a greek bytestring then its a proper utf8
bytestring
filepath = greek_path.decode('utf-8')
[snip]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
On 07/06/2013 08:51, Νικόλαος Κούρας wrote: Finally no suexec erros any more after chown all log files to nobody:nobody and thei corresponding paths. Now the error has been transformed to: [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/koukos.py' failed [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] Premature end of script headers: koukos.py [Fri Jun 07 10:48:47 2013] [error] [client 79.103.41.173] File does not exist: /home/nikos/public_html/500.shtml but from interpretor view: [email protected] [~/www/cgi-bin]# python koukos.py Set-Cookie: nikos=admin; expires=Mon, 02 Jun 2014 07:50:18 GMT; Path=/ Content-type: text/html; charset=utf-8 ΑΠΟ ΔΩ ΚΑΙ ΣΤΟ ΕΞΗΣ ΔΕΝ ΣΕ ΕΙΔΑ, ΔΕΝ ΣΕ ΞΕΡΩ, ΔΕΝ ΣΕ ΑΚΟΥΣΑ! ΘΑ ΕΙΣΑΙ ΠΛΕΟΝ Ο ΑΟΡΑΤΟΣ ΕΠΙΣΚΕΠΤΗΣ!! (2)No such file or directory: exec of '/home/nikos/public_html/cgi-bin/koukos.py' failed Can find what? koukos.py is there inside the cg-bin dir with 755 perms. It's looking for '/home/nikos/public_html/cgi-bin/koukos.py'. Have a look in '/home/nikos/public_html/cgi-bin'. Is 'koukos.py' in there? -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On Fri, 07 Jun 2013 04:53:42 -0700, Νικόλαος Κούρας wrote:
> Do you mean that utf-8, latin-iso, greek-iso and ASCII have the 1st
> 0-127 codepoints similar?
You can answer this yourself. Open a terminal window and start a Python
interactive session. Then try it and see what happens:
s = ''.join(chr(i) for i in range(128))
bytes_as_utf8 = s.encode('utf-8')
bytes_as_latin1 = s.encode('latin-1')
bytes_as_greek_iso = s.encode('ISO-8859-7')
bytes_as_ascii = s.encode('ascii')
bytes_as_utf8 == bytes_as_latin1 == bytes_as_greek_iso == bytes_as_ascii
What result do you get? True or False?
And now you know the answer, without having to ask.
> For example char 'a' has the value of '65' for all of those character
> sets? Is hat what you mean?
You can answer that question yourself.
c = 'a'
for encoding in ('utf-8', 'latin-1', 'ISO-8859-7', 'ascii'):
print(c.encode(encoding))
By the way, I believe that Python has made a strategic mistake in the way
that bytes are printed. I think it leads to more confusion, not less.
Better would be something like this:
c = 'a'
for encoding in ('utf-8', 'latin-1', 'ISO-8859-7', 'ascii'):
print(hex(c.encode(encoding)[0]))
For historical reasons, most (but not all) charsets are supersets of
ASCII. That is, the first 128 characters in the charset are the same as
the 128 characters in ASCII.
> s = 'a' (This is unicode right? Why when we assign a string to a
> variable that string's type is always unicode
Strings in Python 3 are Unicode strings. That's just the way Python
works. Unicode was chosen because Unicode includes over a million
different characters (well, potentially over a million, most of them are
currently unused), and is a strict superset of *all* common legacy
codepages from the old DOS and Windows 95 days.
> and does not automatically
> become utf-8 which includes all available world-wide characters? Unicode
> is something different that a character set? )
Unicode is a character set. It is an enormous set of over one million
characters (technically "code point", but don't worry about the
difference right now) which can be collected in strings.
UTF-8 is an encoding that goes from a string using the Unicode character
set into bytes, and back again. Sometimes, people are lazy and say
"UTF-8" when they mean "Unicode", or visa versa.
UTF-16 and UTF-32 are two different encodings for the same purpose, but
for various technical reasons UTF-8 is better for files.
'λ' is a character which exists in some charsets but not others. It is
not in the ASCII charset, nor is it in Latin-1, nor Big-5. It is in the
ISO-8859-7 charset, and of course it is in Unicode.
In ISO-8859-7, the character 'λ' is stored as byte 0xEB (decimal 235),
just as the character 'a' is stored as byte 0x61 (decimal 97).
In UTF-8, the character λ is stored as two bytes 0xCE 0xBB.
In UTF-16 (big-endian), the character λ is stored as two bytes 0x03 0xBB.
In UTF-32 (big-endian), the character λ is stored as four bytes 0x00 0x00
0x03 0xBB.
That's four different ways of "spelling" the same character as bytes,
just as "three", "trois", "drei", "τρία", "três" are all different ways
of spelling the same number 3.
> utf8_byte = s.encode('utf-8')
>
> Now if we are to decode this back to utf8 we will receive the char 'a'.
> I beleive same thing will happen with latin, greek, ascii isos. Correct?
Why don't you try it for yourself and see?
> The characters that will not decode correctly are those that their
> codepoints are greater that > 127 ?
Maybe, maybe not. It depends on which codepoint, and which encodings.
Some encodings use the same bytes for the same characters. Some encodings
use different bytes. It all depends on the encoding, just like American
and English both spell 3 "three", while French spells it "trois".
> for example if s = 'α' (greek character equivalent to english 'a')
In Latin-1, 'α' does not exist:
py> 'α'.encode('latin-1')
Traceback (most recent call last):
File "", line 1, in
UnicodeEncodeError: 'latin-1' codec can't encode character '\u03b1' in
position 0: ordinal not in range(256)
In the old Windows Greek charset, ISO-8859-7, 'α' is stored as byte 0xE1:
py> 'α'.encode('ISO-8859-7')
b'\xe1'
But in the old Windows *Russian* charset, ISO-8859-5, the byte 0xE1 means
a completely different character, CYRILLIC SMALL LETTER ES:
py> b'\xE1'.decode('ISO-8859-5')
'с'
(don't be fooled that this looks like the English c, it is not the same).
In Unicode, 'α' is always codepoint 0x3B1 (decimal 945):
py> ord('α')
945
but before you can store that on a disk, or as a file name, it needs to
be converted to bytes, and which bytes you get depends on which encoding
you use:
py> 'α'.encode('utf-8')
b'\xce\xb1'
py> 'α'.encode('utf-16be')
b'\x03\xb1'
py> 'α'.encode('utf-32be')
b'\x00\x00\x03\xb1'
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Python Game Development?
I was planning on making a small 2D game in Python. Are there any libraries for this? I know of: • Pygame - As far as I know it's dead and has been for almost a year • PyOgre - Linux and Windows only(I do have those, but I want multi-platform) • Cocos2D - Won't install and cant find any support • PyCap - Can't find any documentation • Panda3D - Dead since 2011 + overkill for what I need • PyOpenGL - Overkill Any help on what to do with this would be appreciated. I am making games mainly in Lua but I'd like to make one in Python for fun. I also understand that Python isn't exactly the *BEST* choice programming a game, but I have heard it is possible. Tell me if it's true. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, Jun 7, 2013 at 9:53 AM, wrote: > I was planning on making a small 2D game in Python. Are there any libraries > for this? I know of: > > • Pygame - As far as I know it's dead and has been for almost a year > • PyOgre - Linux and Windows only(I do have those, but I want multi-platform) > • Cocos2D - Won't install and cant find any support > • PyCap - Can't find any documentation > • Panda3D - Dead since 2011 + overkill for what I need > • PyOpenGL - Overkill > > Any help on what to do with this would be appreciated. I am making games > mainly in Lua but I'd like to make one in Python for fun. I also understand > that Python isn't exactly the *BEST* choice programming a game, but I have > heard it is possible. Tell me if it's true. Thanks! Pygame is still quite commonly used, and the most recent commit was in April, so I think it's too early to pronounce it dead (although pgreloaded, which at one point was intended to be a successor to pygame, is looking a bit dormant now). A lot of folks also like pyglet, but I've never used it myself. I suspect it might also be overkill for you. And yes, it's definitely possible to make games in Python. Go to pyweek.org and check out many of the awesome games that have been developed in Python in only one week. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Friday, June 7, 2013 5:21:36 PM UTC+1, Ian wrote: > On Fri, Jun 7, 2013 at 9:53 AM, wrote: > > > I was planning on making a small 2D game in Python. Are there any libraries > > for this? I know of: > > > > > > • Pygame - As far as I know it's dead and has been for almost a year > > > • PyOgre - Linux and Windows only(I do have those, but I want > > multi-platform) > > > • Cocos2D - Won't install and cant find any support > > > • PyCap - Can't find any documentation > > > • Panda3D - Dead since 2011 + overkill for what I need > > > • PyOpenGL - Overkill > > > > > > Any help on what to do with this would be appreciated. I am making games > > mainly in Lua but I'd like to make one in Python for fun. I also understand > > that Python isn't exactly the *BEST* choice programming a game, but I have > > heard it is possible. Tell me if it's true. Thanks! > > > > Pygame is still quite commonly used, and the most recent commit was in > > April, so I think it's too early to pronounce it dead (although > > pgreloaded, which at one point was intended to be a successor to > > pygame, is looking a bit dormant now). > > > > A lot of folks also like pyglet, but I've never used it myself. I > > suspect it might also be overkill for you. > > > > And yes, it's definitely possible to make games in Python. Go to > > pyweek.org and check out many of the awesome games that have been > > developed in Python in only one week. Do you know of any tutorial for PyGame? Preferably a video tutorial but any tutorial at all is fine! I can't seem to find any, even on pygame.org!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, Jun 7, 2013 at 10:28 AM, Eam onn wrote: > Do you know of any tutorial for PyGame? Preferably a video tutorial but any > tutorial at all is fine! I can't seem to find any, even on pygame.org!!! I'd start here: http://www.pygame.org/wiki/tutorials -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, Jun 7, 2013 at 10:35 AM, Ian Kelly wrote: > On Fri, Jun 7, 2013 at 10:28 AM, Eam onn wrote: >> Do you know of any tutorial for PyGame? Preferably a video tutorial but any >> tutorial at all is fine! I can't seem to find any, even on pygame.org!!! > > I'd start here: http://www.pygame.org/wiki/tutorials Also the section under the "Tutorials" heading at http://www.pygame.org/docs/ -- http://mail.python.org/mailman/listinfo/python-list
Re: trigger at TDM/2 only
DaveA, Yep, that seems to just be about it! Much easier! Thanks for the hint! Much appreciated :) Ron On Thursday, June 6, 2013 5:43:11 PM UTC-7, Dave Angel wrote: > On 06/06/2013 08:03 PM, cerr wrote: > > > Hi, > > > > > > I have a process that I can trigger only at a certain time. Assume I have a > > TDM period of 10min, that means, I can only fire my trigger at the 5th > > minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came > > up with following algorithm which oly leaves the waiting while loop if > > minute % TDM/2 is 0 but not if minute % TDM is 0: > > > min = datetime.datetime.now().timetuple().tm_hour*60 + > > datetime.datetime.now().timetuple().tm_min > > > while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0): > > > > You might have spent three minutes and simplified this for us. And in > > the process discovered the problem. > > > > (BTW, min() is a builtin function, so it's not really a good idea to be > > shadowing it.) > > > > You didn't give python version, so my sample is assuming Python 2.7 > > For your code it shouldn't matter. > > > > tdm = 10 > > tdm2 = 5 > > > > y = min(3,4) > > print y > > > > for now in range(10,32): > > print now, now%tdm, now%tdm2, > > print not(now % tdm !=0 ^ now%tdm2 !=0) #bad > > print not((now % tdm !=0) ^ (now%tdm2 !=0)) #good > > > > > > Your problem is one of operator precedence. Notice that ^ has a higher > > precedence than != operator, so you need the parentheses I added in the > > following line. > > > > What I don't understand is why you used this convoluted approach. Why not > > > > print now%tdm != tdm2 > > > > For precedence rules, see: > >http://docs.python.org/2/reference/expressions.html#operator-precedence > > > > > > > > > > -- > > DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: trigger at TDM/2 only
MRAB,
Thanks for the hint! Yep, that's much easier!
Thanks! :)
Ron
On Thursday, June 6, 2013 5:49:55 PM UTC-7, MRAB wrote:
> On 07/06/2013 01:03, cerr wrote:
>
> > Hi,
>
> >
>
> > I have a process that I can trigger only at a certain time. Assume I have a
> > TDM period of 10min, that means, I can only fire my trigger at the 5th
> > minute of every 10min cycle i.e. at XX:05, XX:15, XX:25... For hat I came
> > up with following algorithm which oly leaves the waiting while loop if
> > minute % TDM/2 is 0 but not if minute % TDM is 0:
>
> > min = datetime.datetime.now().timetuple().tm_hour*60 +
> > datetime.datetime.now().timetuple().tm_min
>
> > while not (min%tdm_timeslot != 0 ^ min%(int(tdm_timeslot/2)) != 0):
>
> > time.sleep(10)
>
> > logger.debug("WAIT
> > "+str(datetime.datetime.now().timetuple().tm_hour*60 +
> > datetime.datetime.now().timetuple().tm_min))
>
> > logger.debug(str(min%(int(tdm_timeslot/2)))+" -
> > "+str(min%tdm_timeslot))
>
> > min = datetime.datetime.now().timetuple().tm_hour*60 +
> > datetime.datetime.now().timetuple().tm_min
>
> > logger.debug("RUN UPDATE CHECK...")
>
> >
>
> > But weird enough, the output I get is something like this:
>
> > I would expect my while to exit the loop as soon as the minute turns
> > 1435... why is it staying in? What am I doing wrong here?
>
> >
>
> > WAIT 1434
>
> > 3 - 3
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1434
>
> > 4 - 4
>
> > WAIT 1435
>
> > 4 - 4
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1435
>
> > 0 - 5
>
> > WAIT 1436
>
> > 0 - 5
>
> > RUN UPDATE CHECK...
>
> >
>
> Possibly it's due to operator precedence. The bitwise operators &, |
>
> and ^ have a higher precedence than comparisons such as !=.
>
>
>
> A better condition might be:
>
>
>
> min % tdm_timeslot != tdm_timeslot // 2
>
>
>
> or, better yet, work out how long before the next trigger time and then
>
> sleep until then.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, 07 Jun 2013 09:28:09 -0700, Eam onn wrote: > Do you know of any tutorial for PyGame? Preferably a video tutorial but > any tutorial at all is fine! I can't seem to find any, even on > pygame.org!!! https://duckduckgo.com/html/?q=pygame+tutorial -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Trying to work with data from a query using Python.
Hello, I'm working with PostgreSQL and Python to obtain 2 columns froma
database and need to print it in a specific format.
Here is my current code.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
con = None
try:
con = psycopg2.connect(database='DB', user='ME', password='1234')
cur = con.cursor()
cur.execute(" select Account_Invoice.amount_untaxed, right
(Res_Partner.vat,length(Res_Partner.vat)-2) as RFC from Account_Invoice inner
join Res_Partner on Account_Invoice.partner_id = Res_Partner.id inner join
Account_Invoice_Tax on Account_Invoice.id = Account_Invoice_Tax.invoice_id
where account_invoice.journal_id=2 and account_invoice.date_invoice >=
'2013-01-01' and account_invoice.date_invoice <= '2013-02-01' and
account_invoice.reconciled is TRUE and account_invoice_tax.account_id = 3237
and account_invoice.amount_tax >= 0;")
rows = cur.fetchall()
for row in rows:
print row
except psycopg2.DatabaseError, e:
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()
Now assume that fetchall would print the following:
LOEL910624ND5 from the column vat as RFC.
227 from the column amount_untaxed.
Now I would need to print that in the following format.
04|85|LOEL910624ND5|227|||
04 always goes in the first column and 85 always goes in the second, vat goes
in the third and the amount_untaxed goes in the eight column but we still need
to have 22 columns in total.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Friday, June 7, 2013 4:53:03 PM UTC+1, Eam onn wrote: > I was planning on making a small 2D game in Python. Are there any libraries > for this? I know of: > > > > • Pygame - As far as I know it's dead and has been for almost a year > > • PyOgre - Linux and Windows only(I do have those, but I want multi-platform) > > • Cocos2D - Won't install and cant find any support > > • PyCap - Can't find any documentation > > • Panda3D - Dead since 2011 + overkill for what I need > > • PyOpenGL - Overkill > > > > Any help on what to do with this would be appreciated. I am making games > mainly in Lua but I'd like to make one in Python for fun. I also understand > that Python isn't exactly the *BEST* choice programming a game, but I have > heard it is possible. Tell me if it's true. Thanks! Pygame isn't too good. You still need a lot of other libraries from what I understand(like for physics). Is there any alternative for 2D? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
You could make a fantastic turtle based game with pyturtle! On Fri, Jun 7, 2013 at 1:53 PM, Eam onn wrote: > On Friday, June 7, 2013 4:53:03 PM UTC+1, Eam onn wrote: > > I was planning on making a small 2D game in Python. Are there any > libraries for this? I know of: > > > > > > > > • Pygame - As far as I know it's dead and has been for almost a year > > > > • PyOgre - Linux and Windows only(I do have those, but I want > multi-platform) > > > > • Cocos2D - Won't install and cant find any support > > > > • PyCap - Can't find any documentation > > > > • Panda3D - Dead since 2011 + overkill for what I need > > > > • PyOpenGL - Overkill > > > > > > > > Any help on what to do with this would be appreciated. I am making games > mainly in Lua but I'd like to make one in Python for fun. I also understand > that Python isn't exactly the *BEST* choice programming a game, but I have > heard it is possible. Tell me if it's true. Thanks! > > Pygame isn't too good. You still need a lot of other libraries from what I > understand(like for physics). Is there any alternative for 2D? > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
Τη Παρασκευή, 7 Ιουνίου 2013 5:32:09 μ.μ. UTC+3, ο χρήστης MRAB έγραψε: >>Can find what? koukos.py is there inside the cg-bin dir with 755 perms. > It's looking for '/home/nikos/public_html/cgi-bin/koukos.py'. Its looking for its self?!?! > Have a look in '/home/nikos/public_html/cgi-bin'. Is 'koukos.py' in > there? Yes it is. [email protected] [~/www/cgi-bin]# ls -l total 56 drwxr-xr-x 2 nikos nikos 4096 Jun 6 20:29 ./ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 ../ -rwxr-xr-x 1 nikos nikos 1199 Apr 25 15:33 convert.py* -rwxr-xr-x 1 nikos nikos 5434 Jun 7 14:51 files.py* -rw-r--r-- 1 nikos nikos170 May 30 15:18 .htaccess -rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py* -rwxr-xr-x 1 nikos nikos 9356 Jun 6 09:13 metrites.py* -rwxr-xr-x 1 nikos nikos 13512 Jun 6 09:13 pelatologio.py* [email protected] [~/www/cgi-bin]# -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to work with data from a query using Python.
On 06/07/2013 01:44 PM, [email protected] wrote: rows = cur.fetchall() for row in rows: print row Now assume that fetchall would print the following: I doubt if fetchall() prints anything. presumably it returns something, extracted from the db. LOEL910624ND5 from the column vat as RFC. 227 from the column amount_untaxed. Now I would need to print that in the following format. 04|85|LOEL910624ND5|227||| 04 always goes in the first column and 85 always goes in the second, vat goes in the third and the amount_untaxed goes in the eight column but we still need to have 22 columns in total. I don't use psycopg2, and I'd suggest few others here do either. Since the problem has nothing to do with psycopg2, could you simplify the problem? Whatever fetchall() returns, it's presumably either a dict or list. Or is it a list of lists? Find out what kind of data it is, and stub it with something like: rows = ["ab", "127"] Then if you define what the items in that list (or whatever) are supposed to mean, we can tell you how to stick all those pipe-symbols between. One likely answer would be the csv module. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, Jun 7, 2013 at 11:53 AM, Eam onn wrote: > Pygame isn't too good. You still need a lot of other libraries from what I > understand(like for physics). Is there any alternative for 2D? I don't know of any Python libraries that provide both a rendering engine and a physics engine. I'm not sure why you say pyame "isn't too good". A library that only does one thing shouldn't be considered a bad library if it does it well. I hear that pygame doesn't scale very well to large games, and it certainly wouldn't be my first choice for a 3D game, but I've never had any major issues with it myself. There's a largish list of alternative libraries that you might consider here: http://wiki.python.org/moin/PythonGameLibraries -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
Τη Παρασκευή, 7 Ιουνίου 2013 5:29:25 μ.μ. UTC+3, ο χρήστης MRAB έγραψε:
> This is a worse way of doing it because the ISO-8859-7 encoding has 1
> byte per codepoint, meaning that it's more 'tolerant' (if that's the
> word) of errors. A sequence of bytes that is actually UTF-8 can be
> decoded as ISO-8859-7, giving gibberish.
> UTF-8 is less tolerant, and it's the encoding that ideally you should
> be using everywhere, so it's better to assume UTF-8 and, if it fails,
> try ISO-8859-7 and then rename so that any names that were ISO-8859-7
> will be converted to UTF-8.
Indeed iw asnt aware of that, at that time, i was under the impression that if
a string was encoded to bytes using soem charset can only be switched back with
the use of that and only that charset. Since this is the case here is my
fixning:
#
# Collect filenames of the path dir as bytes
filename_bytes = os.listdir( b'/home/nikos/public_html/data/apps/' )
for filename in filename_bytes:
# Compute 'path/to/filename' into bytes
filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename'
flag = False
try:
# Assume current file is utf8 encoded
filepath = filepath_bytes.decode('utf-8')
flag = 'utf8'
except UnicodeDecodeError:
try:
# Since current filename is not utf8 encoded then it
has to be greek-iso encoded
filepath = filepath_bytes.decode('iso-8859-7')
flag = 'greek'
except UnicodeDecodeError:
print( '''I give up! File name is unreadable!''' )
if( flag = 'greek' )
# Rename filename from greek bytes --> utf-8 bytes
os.rename( filepath_bytes, filepath.encode('utf-8') )
#
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
# Load'em
for filename in filenames:
try:
# Check the presence of a file against the database and insert
if it doesn't exist
cur.execute('''SELECT url FROM files WHERE url = %s''',
filename )
data = cur.fetchone()
if not data:
# First time for file; primary key is automatic, hit is
defaulted
cur.execute('''INSERT INTO files (url, host, lastvisit)
VALUES (%s, %s, %s)''', (filename, host, lastvisit) )
except pymysql.ProgrammingError as e:
print( repr(e) )
#
filenames = os.listdir( '/home/nikos/public_html/data/apps/' )
filepaths = ()
# Build a set of 'path/to/filename' based on the objects of path dir
for filename in filenames:
filepaths.add( filename )
# Delete spurious
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()
# Check database's filenames against path's filenames
for rec in data:
if rec not in filepaths:
cur.execute('''DELETE FROM files WHERE url = %s''', rec )
=
[email protected] [~/www/cgi-bin]# [Fri Jun 07 21:49:33 2013] [error] [client
79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", line 81
[Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag ==
'greek' )
[Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173]
^
[Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: invalid
syntax
[Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of
script headers: files.py
---
i dont know why that if statement errors.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Trying to work with data from a query using Python.
[email protected] wrote: > Hello, I'm working with PostgreSQL and Python to obtain 2 columns froma > database and need to print it in a specific format. > > Here is my current code. > > > > #!/usr/bin/python > # -*- coding: utf-8 -*- > > import psycopg2 > import sys > > con = None > > try: > > con = psycopg2.connect(database='DB', user='ME', password='1234') > > cur = con.cursor() > cur.execute(" select Account_Invoice.amount_untaxed, right > (Res_Partner.vat,length(Res_Partner.vat)-2) as RFC from > Account_Invoice inner join Res_Partner on Account_Invoice.partner_id = > Res_Partner.id inner join Account_Invoice_Tax on Account_Invoice.id = > Account_Invoice_Tax.invoice_id where account_invoice.journal_id=2 and > account_invoice.date_invoice >= '2013-01-01' and > account_invoice.date_invoice <= '2013-02-01' and > account_invoice.reconciled is TRUE and account_invoice_tax.account_id > = 3237 and account_invoice.amount_tax >= 0;") > > rows = cur.fetchall() > > for row in rows: > print row > > > except psycopg2.DatabaseError, e: > print 'Error %s' % e > sys.exit(1) > > > finally: > > if con: > con.close() > > > > > Now assume that fetchall would print the following: > > LOEL910624ND5 from the column vat as RFC. > 227 from the column amount_untaxed. > > > Now I would need to print that in the following format. > > 04|85|LOEL910624ND5|227||| > > 04 always goes in the first column and 85 always goes in the second, vat > goes in the third and the amount_untaxed goes in the eight column but we > still need to have 22 columns in total. Keep it simple: COLUMN_COUNT = 22 TEMPLATE = "04|85|{0}|{1}|||" assert TEMPLATE.count("|") == COLUMN_COUNT -1, "You cannot count ;)" for row in cur.fetchall(): print TEMPLATE.format(*row) A bit more general: fill_rows(rows): out_row = [""] * 22 out_row[0] = "04" out_row[1] = "85" for row in rows: out_row[2], out_row[7] = row # copying not necessary here, but let's play it safe yield out_row[:] writer = csv.writer(sys.stdout, delimiter="|") writer.writerows(fill_rows(cur.fetchall())) All untested code. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
: On 7 June 2013 14:52, Νικόλαος Κούρας wrote: File "/home/nikos/public_html/cgi-bin/files.py", line 81 > [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == > 'greek' ) > [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] > ^ > [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: > invalid syntax > [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of > script headers: files.py > --- > i dont know why that if statement errors. Oh for f... READ SOME DOCUMENTATION, FOR THE LOVE OF BOB!!! READ YOUR OWN EFFING CODE! Look at this: http://docs.python.org/2/tutorial/controlflow.html Read it now? Of course not. Go away and read it. Now have you read it? GO AND READ IT. What does an if statement end with? Hint: yep, that's it. -[]z. -- http://mail.python.org/mailman/listinfo/python-list
Oportunidade: Desenvolvedor Python
Fabrica de software localizada na Barra da Tijuca contrata: Desenvolvedor Python Objetivo geral da Posição: Desenvolvimento de sistemas Web com Python/Django, HTML5, Javascript e CSS. Prérequisitos: Experiência com Python/Django ou outro framework MVC. Familiarizado com desenvolvimento frontend: padrões Web, (X)HTML, HTML5, CSS, javas (jQuery). Conhecimentos de linguagem SQL e Stored Procedures Conhecimentos de bancos de dados MySQL / Postgresql Conhecimentos de padrões de projeto (design patterns) Interessante como diferenciais: Computer Vision (OpenCV) Outras linguagens: PHP, C/C++ Desenvolvimento para smartphones: iOS e Android TDD Agile development (Scrum, XP, etc.) SO Linux/Unix like Desenvolvimento para Android e iOS Bancos de dados NoSQL (ex: MongoDB) API do Facebook / Twitter Formação: Nenhuma formação específica é obrigatória, entretanto podem ser considerado um diferencial. Deveres: Desenvolver seguindo o padrão de qualidade esperado, manter sincronia com a equipe, estima cumprir prazos de entregas. A empresa oferece salário compatível com o mercado + Benefícios (Assistência Médica + Assistência Odontológica + Bônus por Resultado + Vale Transporte). Os interessados deverão enviar o CV para [email protected] , mencionando no assunto Desenvolvedor Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07/06/2013 20:31, Zero Piraeus wrote: : On 7 June 2013 14:52, Νικόλαος Κούρας wrote: File "/home/nikos/public_html/cgi-bin/files.py", line 81 [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == 'greek' ) [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] ^ [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: invalid syntax [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of script headers: files.py --- i dont know why that if statement errors. Oh for f... READ SOME DOCUMENTATION, FOR THE LOVE OF BOB!!! READ YOUR OWN EFFING CODE! Look at this: http://docs.python.org/2/tutorial/controlflow.html Read it now? Of course not. Go away and read it. Now have you read it? GO AND READ IT. What does an if statement end with? Hint: yep, that's it. Have you noticed how the line in the traceback doesn't match the line in the post? -- http://mail.python.org/mailman/listinfo/python-list
[ANNOUNCE] greenlet 0.4.1
Hi, I have uploaded greenlet 0.4.1 to PyPI: https://pypi.python.org/pypi/greenlet What is it? --- The greenlet module provides coroutines for python. coroutines allow suspending and resuming execution at certain locations. concurrence[1], eventlet[2] and gevent[3] use the greenlet module in order to implement concurrent network applications. Documentation can be found here: http://greenlet.readthedocs.org The code is hosted on github: https://github.com/python-greenlet/greenlet Changes in version 0.4.1 The NEWS file lists these changes for release 0.4.1: * fix segfaults when using gcc 4.8 on amd64/x86 unix * try to disable certain gcc 4.8 optimizations that make greenlet crash * Fix greenlet on aarch64 with gcc 4.8 * workaround segfault on SunOS/sun4v * Add support for Aarch64 * Add support for x32 psABI on x86_64 * Changed memory constraints for assembly macro for PPC Linux platforms. Many thanks to all contributors! [1] http://opensource.hyves.org/concurrence/ [2] http://eventlet.net/ [3] http://www.gevent.org/ -- Cheers Ralf Schmitt -- http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
On 07/06/2013 19:24, Νικόλαος Κούρας wrote: Τη Παρασκευή, 7 Ιουνίου 2013 5:32:09 μ.μ. UTC+3, ο χρήστης MRAB έγραψε: Can find what? koukos.py is there inside the cg-bin dir with 755 perms. It's looking for '/home/nikos/public_html/cgi-bin/koukos.py'. Its looking for its self?!?! Have a look in '/home/nikos/public_html/cgi-bin'. Is 'koukos.py' in there? Yes it is. [email protected] [~/www/cgi-bin]# ls -l total 56 drwxr-xr-x 2 nikos nikos 4096 Jun 6 20:29 ./ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 ../ -rwxr-xr-x 1 nikos nikos 1199 Apr 25 15:33 convert.py* -rwxr-xr-x 1 nikos nikos 5434 Jun 7 14:51 files.py* -rw-r--r-- 1 nikos nikos170 May 30 15:18 .htaccess -rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py* -rwxr-xr-x 1 nikos nikos 9356 Jun 6 09:13 metrites.py* -rwxr-xr-x 1 nikos nikos 13512 Jun 6 09:13 pelatologio.py* [email protected] [~/www/cgi-bin]# The prompt says "~/www/cgi-bin". Is that the same as "/home/nikos/public_html/cgi-bin"? Try: ls -l /home/nikos/public_html/cgi-bin -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On Fri, Jun 7, 2013 at 8:53 AM, wrote: > I also understand that Python isn't exactly the *BEST* choice programming > a game, but I have heard it is possible. Tell me if it's true. Thanks! > One of the Blizzard people told me that it's very common to program game logic in Python, with the 3D stuff done in C wrapped up to be callable from Python. But I've since heard that Blizzard mostly uses Lua, from someone who doesn't work at Blizzard. Maybe Blizzard use both Python and Lua, or maybe they changed, I don't know. -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to work with data from a query using Python.
On Fri, 07 Jun 2013 14:24:30 -0400, Dave Angel wrote: > On 06/07/2013 01:44 PM, [email protected] wrote: >> > >> >> rows = cur.fetchall() >> >> for row in rows: >> print row >> >> >> >> >> Now assume that fetchall would print the following: > > I doubt if fetchall() prints anything. presumably it returns something, > extracted from the db. > > >> LOEL910624ND5 from the column vat as RFC. >> 227 from the column amount_untaxed. >> >> >> Now I would need to print that in the following format. >> >> 04|85|LOEL910624ND5|227||| >> >> 04 always goes in the first column and 85 always goes in the second, >> vat goes in the third and the amount_untaxed goes in the eight column >> but we still need to have 22 columns in total. >> >> >> > > I don't use psycopg2, and I'd suggest few others here do either. > > Since the problem has nothing to do with psycopg2, could you simplify > the problem? Whatever fetchall() returns, it's presumably either a dict > or list. Or is it a list of lists? > It actually returns a list of tuples. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
: On 7 June 2013 16:45, MRAB wrote: > On 07/06/2013 20:31, Zero Piraeus wrote: >> [something exasperated, in capitals] > > Have you noticed how the line in the traceback doesn't match the line > in the post? Actually, I hadn't. It's not exactly a surprise at this point, though ... I learnt a new word today, while searching for an apt ending to the sentence "Reading Nikos' posts is the internet equivalent of ..." ... and that word is Dermatillomania. -[]z. -- http://mail.python.org/mailman/listinfo/python-list
Re: [ANNOUNCE] greenlet 0.4.1
In article , Ralf Schmitt wrote: > Hi, > > I have uploaded greenlet 0.4.1 to PyPI: > https://pypi.python.org/pypi/greenlet > > What is it? > --- > The greenlet module provides coroutines for python. coroutines allow > suspending and resuming execution at certain locations. > > concurrence[1], eventlet[2] and gevent[3] use the greenlet module in > order to implement concurrent network applications. We use gevent at Songza. It's very cool. We went from 50 gunicorn workers per server to 8 when we switched to gevent. -- http://mail.python.org/mailman/listinfo/python-list
Idiomatic Python for incrementing pairs
Playing around, I've been trying to figure out the most pythonic way of incrementing multiple values based on the return of a function. Something like def calculate(params): a = b = 0 if some_calculation(params): a += 1 if other_calculation(params): b += 1 return (a, b) alpha = beta = 0 temp_a, temp_b = calculate(...) alpha += temp_a beta += temp_b Is there a better way to do this without holding each temporary result before using it to increment? -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07Jun2013 11:52, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= wrote: | [email protected] [~/www/cgi-bin]# [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", line 81 | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == 'greek' ) | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] ^ | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: invalid syntax | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of script headers: files.py | --- | i dont know why that if statement errors. Python statements that continue (if, while, try etc) end in a colon, so: if flag == 'greek': Cheers, -- Cameron Simpson Hello, my name is Yog-Sothoth, and I'll be your eldritch horror today. - Heather Keith -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
On 07Jun2013 04:53, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?=
wrote:
| Τη Παρασκευή, 7 Ιουνίου 2013 11:53:04 π.μ. UTC+3, ο χρήστης Cameron Simpson
έγραψε:
| > | >| errors='replace' mean dont break in case or error?
| >
| > | >Yes. The result will be correct for correct iso-8859-7 and slightly
mangled
| > | >for something that would not decode smoothly.
| >
| > | How can it be correct? We have encoded out string in utf-8 and then
| > | we tried to decode it as greek-iso? How can this possibly be
| > | correct?
|
| > If it is a valid iso-8859-7 sequence (which might cover everything,
| > since I expect it is an 8-bit 1:1 mapping from bytes values to a
| > set of codepoints, just like iso-8859-1) then it may decode to the
| > "wrong" characters, but the reverse process (characters encoded as
| > bytes) should produce the original bytes. With a mapping like this,
| > errors='replace' may mean nothing; there will be no errors because
| > the only Unicode characters in play are all from iso-8859-7 to start
| > with. Of course another string may not be safe.
|
| > Visually, the names will be garbage. And if you go:
| > mv '999-EΟΟΞ�-ΟΞΏΟ-ΞΞ·ΟΞΏΟ.mp3' '999-Eυχή-του-Ιησού.mp3'
| > while using the iso-8859-7 locale, the wrong thing will occur
| > (assuming it even works, though I think it should because all these
| > characters are represented in iso-8859-7, yes?)
|
| All the rest you i understood only the above quotes its still unclear to me.
| I cant see to understand it.
|
| Do you mean that utf-8, latin-iso, greek-iso and ASCII have the 1st 0-127
codepoints similar?
Yes. It is certainly true for utf-8 and latin-iso and ASCII.
I expect it to be so for greek-iso, but have not checked.
They're all essentially the ASCII set plus a range of other character
codepoints for the upper values. The 8-bit sets iso-8859-1 (which
I take you to mean by "latin-iso") and iso-8859-7 (which I take you
to mean by "greek-iso") are single byte mapping with the top half
mapped to characters commonly used in a particular region.
Unicode has a much greater range, but the UTF-8 encoding of Unicode
deliberately has the bottom 0-127 identical to ASCII, and higher
values represented by multibyte sequences commences with at least
the first byte in the 128-255 range. In this way pure ASCII files
are already in UTF-8 (and, in fact, work just fine for the iso-8859-x
encodings as well).
| For example char 'a' has the value of '65' for all of those character sets?
| Is hat what you mean?
Yes.
| s = 'a' (This is unicode right? Why when we assign a string to
| a variable that string's type is always unicode and does not
| automatically become utf-8 which includes all available world-wide
| characters? Unicode is something different that a character set? )
In Python 3, yes. Strings are unicode. Note that that means they are
sequences of codepoints whose meaning is as for Unicode.
"utf-8" is a byte encoding for Unicode strings. An external storage
format, if you like. The first 0-127 codepoints are 1:1 with byte
values, and the higher code points require multibyte sequences.
| utf8_byte = s.encode('utf-8')
Unicode string => utf-8 byte encoding.
| Now if we are to decode this back to utf8 we will receive the char 'a'.
Yes.
| I beleive same thing will happen with latin, greek, ascii isos. Correct?
|
| utf8_a = utf8_byte.decode('iso-8859-7')
| latin_a = utf8_byte.decode('iso-8859-1')
| ascii_a = utf8_byte.decode('ascii')
| utf8_a = utf8_byte.decode('iso-8859-7')
|
| Is this correct?
Yes, because of the design decision about the 0-127 codepoints.
| All of those decodes will work even if the encoded bytestring was of utf8
type?
|
| The characters that will not decode correctly are those that their codepoints
are greater that > 127 ?
| for example if s = 'α' (greek character equivalent to english 'a')
| Is this what you mean?
Yes, exactly so.
|
|
| Now back to my almost ready files.py script please:
|
|
| #
| # Collect filenames of the path dir as bytes
| greek_filenames = os.listdir( b'/home/nikos/public_html/data/apps/' )
|
| for filename in greek_filenames:
| # Compute 'path/to/filename' in bytes
| greek_path = b'/home/nikos/public_html/data/apps/' + b'filename'
You don't mean b'filename', which is the literal word "filename".
You mean: filename.encode('iso-8859-7')
More probably, you mean:
dirpath = b'/home/nikos/public_html/data/apps/'
greek_filenames = os.listdir(dirpath)
for greek_filename in greek_filenames:
try:
filename = greek_filename.decode('iso-8859-7')
and then:
greek_path = dirpath + greek_filename
utf8_filename = filename.encode('utf-8')
utf8_path = dirpath + utf8_filename
| try:
| filepath = greek_path.decode('iso-8859-7')
| # Rename current filename from greek bytes --> utf-8 bytes
| os.rename( greek_path, filepath.e
Re: Idiomatic Python for incrementing pairs
On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase wrote: > Playing around, I've been trying to figure out the most pythonic way > of incrementing multiple values based on the return of a function. > Something like > > def calculate(params): > a = b = 0 > if some_calculation(params): > a += 1 > if other_calculation(params): > b += 1 > return (a, b) > > alpha = beta = 0 > temp_a, temp_b = calculate(...) > alpha += temp_a > beta += temp_b > > Is there a better way to do this without holding each temporary > result before using it to increment? > alpha = beta = 0 alpha, beta = (sum(x) for x in zip( (alpha, beta), calculate(...) ) ) It saves a couple lines of code, but at the expense of readability IMO. If I was reading the first, I'd know exactly what was happening immediately. If I was reading the second, it would take a bit to decipher. In this example, I don't see a better solution to what you're doing. All the best, Jason -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiomatic Python for incrementing pairs
On Sat, Jun 8, 2013 at 12:32 PM, Tim Chase wrote: > def calculate(params): > a = b = 0 > if some_calculation(params): > a += 1 > if other_calculation(params): > b += 1 > return (a, b) > > alpha = beta = 0 > temp_a, temp_b = calculate(...) > alpha += temp_a > beta += temp_b > > Is there a better way to do this without holding each temporary > result before using it to increment? Can you pass the function a list with the appropriate values in them, and have it return them via that? def calculate(params, sums): if some_calculation(params): sums[0] += 1 if other_calculation(params): sums[1] += 1 sums = [0, 0] calculate(..., sums) Or use a dictionary if you'd rather they have names, either way. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
RE: Idiomatic Python for incrementing pairs
alpha, beta = (1 if some_calculation(params) else 0, 1 if other_calculation(params) else 0) > Date: Fri, 7 Jun 2013 21:32:39 -0500 > From: [email protected] > To: [email protected] > Subject: Idiomatic Python for incrementing pairs > > Playing around, I've been trying to figure out the most pythonic way > of incrementing multiple values based on the return of a function. > Something like > > def calculate(params): > a = b = 0 > if some_calculation(params): > a += 1 > if other_calculation(params): > b += 1 > return (a, b) > > alpha = beta = 0 > temp_a, temp_b = calculate(...) > alpha += temp_a > beta += temp_b > > Is there a better way to do this without holding each temporary > result before using it to increment? > > -tkc > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiomatic Python for incrementing pairs
On 2013-06-07 23:46, Jason Swails wrote: > On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase > > def calculate(params): > > a = b = 0 > > if some_calculation(params): > > a += 1 > > if other_calculation(params): > > b += 1 > > return (a, b) > > > > alpha = beta = 0 > > temp_a, temp_b = calculate(...) > > alpha += temp_a > > beta += temp_b > > > > Is there a better way to do this without holding each temporary > > result before using it to increment? > > alpha = beta = 0 > alpha, beta = (sum(x) for x in zip( (alpha, beta), > calculate(...) ) ) Yeah, I came up with something similar, but it was so opaque I fell back to the more readable version I had above. With only the two variables to increment in this case, the overhead of duplicating code doesn't seem as worth it as it might be if there were umpteen counters being returned as a tuple and umpteen corresponding variables being updated (at which point, it might make more sense to switch to another data structure like a dict). Ah well. Glad to see at least I'm not the only one stymied by trying to make it more pythonic while at least keeping it readable. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiomatic Python for incrementing pairs
On 2013-06-08 07:04, Carlos Nepomuceno wrote: > alpha, beta = (1 if some_calculation(params) else 0, 1 if > other_calculation(params) else 0) This one sets them to absolute values, rather than the incrementing functionality in question: > > alpha += temp_a > > beta += temp_b The actual code in question does the initialization outside a loop: alphas_updated = betas_updated = 0 for thing in bunch_of_things: a, b = process(thing) alphas_updated += a betas_updated += b and it just bugs me as being a little warty for having temp variables when Python does things like tuple-unpacking so elegantly. That said, as mentioned in a contemporaneous reply to Jason, I haven't found anything better that is still readable. -tkc -- http://mail.python.org/mailman/listinfo/python-list
RE: Idiomatic Python for incrementing pairs
Oh! I really though you were just adding 1 or 0 to those variables. In clude the loop next time! ;) You can accumulate the values by doing this instead: alpha, beta = (alpha + (1 if some_calculation(params) else 0), beta + (1 if other_calculation(params) else 0)) > Date: Fri, 7 Jun 2013 23:16:22 -0500 > From: [email protected] > To: [email protected] > CC: [email protected] > Subject: Re: Idiomatic Python for incrementing pairs > > On 2013-06-08 07:04, Carlos Nepomuceno wrote: > > alpha, beta = (1 if some_calculation(params) else 0, 1 if > > other_calculation(params) else 0) > > This one sets them to absolute values, rather than the incrementing > functionality in question: > > > > alpha += temp_a > > > beta += temp_b > > The actual code in question does the initialization outside a loop: > > alphas_updated = betas_updated = 0 > for thing in bunch_of_things: > a, b = process(thing) > alphas_updated += a > betas_updated += b > > and it just bugs me as being a little warty for having temp > variables when Python does things like tuple-unpacking so elegantly. > That said, as mentioned in a contemporaneous reply to Jason, I haven't > found anything better that is still readable. > > -tkc > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On 06/07/2013 09:28 AM, Eam onn wrote: On Friday, June 7, 2013 5:21:36 PM UTC+1, Ian wrote: On Fri, Jun 7, 2013 at 9:53 AM, wrote: Do you know of any tutorial for PyGame? Preferably a video tutorial but any tutorial at all is fine! I can't seem to find any, even on pygame.org!!! Check out: http://inventwithpython.com/pygame/ for the book "Making Games with Python & Pygame" You can buy it, read it on-line for free, or download the pdf or eReader versions for free. (Totally irrelevant side-comment: PyGame and all the games from this book come pre-loaded on the Raspberry Pi.) -=- Larry -=- -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Game Development?
On 07/06/13 16:53, [email protected] wrote: I was planning on making a small 2D game in Python. Are there any libraries for this? I know of: • Pygame - As far as I know it's dead and has been for almost a year • PyOgre - Linux and Windows only(I do have those, but I want multi-platform) • Cocos2D - Won't install and cant find any support • PyCap - Can't find any documentation • Panda3D - Dead since 2011 + overkill for what I need • PyOpenGL - Overkill Any help on what to do with this would be appreciated. I am making games mainly in Lua but I'd like to make one in Python for fun. I also understand that Python isn't exactly the *BEST* choice programming a game, but I have heard it is possible. Tell me if it's true. Thanks! You might also wish to consider Kivy (kivy.org). Regards, Ian F -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiomatic Python for incrementing pairs
On Fri, 07 Jun 2013 21:32:39 -0500, Tim Chase wrote: > Playing around, I've been trying to figure out the most pythonic way of > incrementing multiple values based on the return of a function. > Something like [...skip misleading and irrelevant calculate() function...] > alpha = beta = 0 > temp_a, temp_b = calculate(...) > alpha += temp_a > beta += temp_b > > Is there a better way to do this without holding each temporary result > before using it to increment? Not really. The above idiom is not really terribly Pythonic. It's more like the sort of old-fashioned procedural code I'd write in Pascal or COBOL or similar. For just two variables, it's not so bad, although I'd probably save a line and a temporary variable and write it as this: alpha = beta = 0 tmp = calculate(...) alpha, beta = alpha+tmp[0], beta+tmp[1] But if you have many such values, that's a sign that you're doing it wrong. Do it like this instead: values = [0]*17 # or however many variables you have increments = calculate(...) values = [a+b for a,b in zip(values, increments)] Or define a helper function: add(v1, v2): """Vector addition. >>> add([1, 2], [4, 5]) [5, 7] """ return [a+b for a,b in zip(v1, v2)] values = [0]*17 increments = calculate(...) values = add(values, increments) Much nicer! And finally, if speed is absolutely critical, this scales to using fast vector libraries like numpy. Just use numpy arrays instead of lists, and + instead of the add helper function, and Bob's yer uncle. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiomatic Python for incrementing pairs
Tim Chase wrote: > On 2013-06-07 23:46, Jason Swails wrote: >> On Fri, Jun 7, 2013 at 10:32 PM, Tim Chase >> > def calculate(params): >> > a = b = 0 >> > if some_calculation(params): >> > a += 1 >> > if other_calculation(params): >> > b += 1 >> > return (a, b) >> > >> > alpha = beta = 0 >> > temp_a, temp_b = calculate(...) >> > alpha += temp_a >> > beta += temp_b >> > >> > Is there a better way to do this without holding each temporary >> > result before using it to increment? >> >> alpha = beta = 0 >> alpha, beta = (sum(x) for x in zip( (alpha, beta), >> calculate(...) ) ) > > Yeah, I came up with something similar, but it was so opaque I fell > back to the more readable version I had above. With only the two > variables to increment in this case, the overhead of duplicating code > doesn't seem as worth it as it might be if there were umpteen > counters being returned as a tuple and umpteen corresponding > variables being updated (at which point, it might make more sense to > switch to another data structure like a dict). Ah well. Glad to see > at least I'm not the only one stymied by trying to make it more > pythonic while at least keeping it readable. > > -tkc You can hide the complexity in a custom class: >>> class T(tuple): ... def __add__(self, other): ... return T((a+b) for a, b in zip(self, other)) ... >>> t = T((0, 0)) >>> for pair in [(1, 10), (2, 20), (3, 30)]: ... t += pair ... >>> t (6, 60) (If you are already using numpy you can do the above with a numpy.array instead of writing your own T.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing filenames from Greeklish => Greek (subprocess complain)
Τη Σάββατο, 8 Ιουνίου 2013 5:52:22 π.μ. UTC+3, ο χρήστης Cameron Simpson έγραψε: > On 07Jun2013 11:52, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= > wrote: > > | [email protected] [~/www/cgi-bin]# [Fri Jun 07 21:49:33 2013] [error] > [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", > line 81 > > | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == > 'greek' ) > > | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] > ^ > > | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: > invalid syntax > > | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of > script headers: files.py > > | --- > > | i dont know why that if statement errors. > > > > Python statements that continue (if, while, try etc) end in a colon, so: Oh iam very sorry. Oh my God i cant beleive i missed a colon *again*: I have corrected this: # # Collect filenames of the path dir as bytes filename_bytes = os.listdir( b'/home/nikos/public_html/data/apps/' ) for filename in filename_bytes: # Compute 'path/to/filename' into bytes filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename' flag = False try: # Assume current file is utf8 encoded filepath = filepath_bytes.decode('utf-8') flag = 'utf8' except UnicodeDecodeError: try: # Since current filename is not utf8 encoded then it has to be greek-iso encoded filepath = filepath_bytes.decode('iso-8859-7') flag = 'greek' except UnicodeDecodeError: print( '''I give up! File name is unreadable!''' ) if flag == 'greek': # Rename filename from greek bytes --> utf-8 bytes os.rename( filepath_bytes, filepath.encode('utf-8') ) == Now everythitng were supposed to work but instead iam getting this surrogate error once more. What is this surrogate thing? Since i make use of error cathcing and handling like 'except UnicodeDecodeError:' then it utf8's decode fails for some reason, it should leave that file alone and try the next file? try: # Assume current file is utf8 encoded filepath = filepath_bytes.decode('utf-8') flag = 'utf8' except UnicodeDecodeError: This is what it supposed to do, correct? == [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", line 94, in [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] cur.execute('''SELECT url FROM files WHERE url = %s''', (filename,) ) [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] File "/usr/local/lib/python3.3/site-packages/PyMySQL3-0.5-py3.3.egg/pymysql/cursors.py", line 108, in execute [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] query = query.encode(charset) [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] UnicodeEncodeError: 'utf-8' codec can't encode character '\\udcce' in position 35: surrogates not allowed -- http://mail.python.org/mailman/listinfo/python-list
Re: Errin when executing a cgi script that sets a cookie in the browser
Τη Παρασκευή, 7 Ιουνίου 2013 11:47:58 μ.μ. UTC+3, ο χρήστης MRAB έγραψε: > On 07/06/2013 19:24, Νικόλαος Κούρας wrote: > > > Τη Παρασκευή, 7 Ιουνίου 2013 5:32:09 μ.μ. UTC+3, ο χρήστης MRAB έγραψε: > > >>>Can find what? koukos.py is there inside the cg-bin dir with 755 perms. > > > > > >> It's looking for '/home/nikos/public_html/cgi-bin/koukos.py'. > > > > > > Its looking for its self?!?! > > > > > >> Have a look in '/home/nikos/public_html/cgi-bin'. Is 'koukos.py' in > > >> there? > > > Yes it is. > > > > > > > > > [email protected] [~/www/cgi-bin]# ls -l > > > total 56 > > > drwxr-xr-x 2 nikos nikos 4096 Jun 6 20:29 ./ > > > drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 ../ > > > -rwxr-xr-x 1 nikos nikos 1199 Apr 25 15:33 convert.py* > > > -rwxr-xr-x 1 nikos nikos 5434 Jun 7 14:51 files.py* > > > -rw-r--r-- 1 nikos nikos170 May 30 15:18 .htaccess > > > -rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py* > > > -rwxr-xr-x 1 nikos nikos 9356 Jun 6 09:13 metrites.py* > > > -rwxr-xr-x 1 nikos nikos 13512 Jun 6 09:13 pelatologio.py* > > > [email protected] [~/www/cgi-bin]# > > > > > The prompt says "~/www/cgi-bin". > > > > Is that the same as "/home/nikos/public_html/cgi-bin"? > > > > Try: > > > > ls -l /home/nikos/public_html/cgi-bin Good day MRAB, yes '~/www' its a symlink to '~/public_html' [email protected] [~/www/data/apps]# ls -ld /home/nikos/www/ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 /home/nikos/www// [email protected] [~/www/data/apps]# ls -ld /home/nikos/public_html/ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 /home/nikos/public_html// [email protected] [~/www/data/apps]# ls -l /home/nikos/public_html/cgi-bin total 56 drwxr-xr-x 2 nikos nikos 4096 Jun 6 20:29 ./ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 ../ -rwxr-xr-x 1 nikos nikos 1199 Apr 25 15:33 convert.py* -rwxr-xr-x 1 nikos nikos 5793 Jun 8 09:39 files.py* -rw-r--r-- 1 nikos nikos170 May 30 15:18 .htaccess -rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py* -rwxr-xr-x 1 nikos nikos 9356 Jun 6 09:13 metrites.py* -rwxr-xr-x 1 nikos nikos 13512 Jun 6 09:13 pelatologio.py* [email protected] [~/www/data/apps]# ls -l /home/nikos/www/cgi-bin/ total 56 drwxr-xr-x 2 nikos nikos 4096 Jun 6 20:29 ./ drwxr-x--- 4 nikos nobody 4096 Jun 5 11:32 ../ -rwxr-xr-x 1 nikos nikos 1199 Apr 25 15:33 convert.py* -rwxr-xr-x 1 nikos nikos 5793 Jun 8 09:39 files.py* -rw-r--r-- 1 nikos nikos170 May 30 15:18 .htaccess -rwxr-xr-x 1 nikos nikos 1160 Jun 6 06:27 koukos.py* -rwxr-xr-x 1 nikos nikos 9356 Jun 6 09:13 metrites.py* -rwxr-xr-x 1 nikos nikos 13512 Jun 6 09:13 pelatologio.py* [email protected] [~/www/data/apps]# -- http://mail.python.org/mailman/listinfo/python-list
